Created
December 8, 2015 19:34
-
-
Save dima-starosud/7274dfd900c8f7ecbf2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Impl do | |
defstruct value: 100 | |
def runImpl(%Impl{value: value}, x, y) do | |
value + x + y | |
end | |
end | |
defprotocol Runnable do | |
def run(r, x, y) | |
end | |
defimpl Runnable, for: Impl do | |
def run(r, x, y) do | |
Impl.runImpl(r, x, y) | |
end | |
end | |
defmodule ProtoTest do | |
use ExUnit.Case | |
@count 1000_000 | |
defmacrop measure(name, block) do | |
quote do | |
{micros, _} = :timer.tc fn -> | |
Enum.each 1..@count, fn _ -> | |
unquote(block) | |
end | |
end | |
micros = micros / @count | |
IO.write "Execution of #{unquote(name)} took #{inspect micros} microseconds\n" | |
end | |
end | |
test "how fast?" do | |
r = %Impl{} | |
measure "protocol" do | |
Runnable.run(r, 10, 20) | |
end | |
f = &(Impl.runImpl(r, &1, &2)) | |
measure "closure" do | |
f.(10, 20) | |
end | |
measure "direct" do | |
Impl.runImpl(r, 10, 20) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment