Skip to content

Instantly share code, notes, and snippets.

@krestenkrab
Created September 12, 2013 12:00
Show Gist options
  • Save krestenkrab/6536257 to your computer and use it in GitHub Desktop.
Save krestenkrab/6536257 to your computer and use it in GitHub Desktop.
Testing performance for elixir protocol dispatch for is_record
R16B01 @ x86_64-apple-darwin12.4.0, 1000000 iterations
when matching: binary is 55.46651865727732% faster
when not matching: binary is 17.42297571085909% faster
[{214705,'Foo.List'},
{177297,'Foo.Bin'},
{345924,'Elixir.List'},
{154052,'Elixir.Bin'}]
R15B03 @ i386-apple-darwin10.8.0, 1000000 iterations
when matching: binary is 29.912398410005824% faster
when not matching: binary is 4.686099611625627% faster
[{46862,'Foo.List'},
{44666,'Foo.Bin'},
{78994,'Elixir.List'},
{55365,'Elixir.Bin'}]
R16B01 @ java, 1000000 iterations
when matching: binary is -51.698619960570305% faster
when not matching: binary is -32.230693921143974% faster
[{67414,'Foo.List'},
{89142,'Foo.Bin'},
{104997,'Elixir.List'},
{159279,'Elixir.Bin'}]
-module(test).
-export([main/0, main/1]).
main() ->
main(1000).
main(N) ->
T1 = {ListNo,_} = timer:tc(fun test_list/2, ['Foo.List', N]),
T2 = {BinNo,_} = timer:tc(fun test_bin/2, ['Foo.Bin', N]),
T3 = {ListOk,_} = timer:tc(fun test_list/2, ['Elixir.List', N]),
T4 = {BinOk,_} = timer:tc(fun test_bin/2, ['Elixir.Bin', N]),
io:format("~s @ ~s, ~p iterations~n",
[erlang:system_info(otp_release),
erlang:system_info(system_architecture),
N]),
io:format(" when matching: binary is ~p% faster~n", [ 100 * ( 1.0 - (BinOk / ListOk)) ]),
io:format("when not matching: binary is ~p% faster~n", [ 100 * ( 1.0 - (BinNo / ListNo)) ]),
[T1,T2,T3,T4].
test_list(Atom, 0) ->
Atom;
test_list(Atom, N) ->
case atom_to_list(Atom) of
"Elixir." ++ _ ->
test_list(Atom, N-1);
_ ->
test_list(Atom, N-1)
end.
test_bin(Atom, 0) ->
Atom;
test_bin(Atom, N) ->
case atom_to_binary(Atom, latin1) of
<<"Elixir.", _/binary>> ->
test_bin(Atom, N-1);
_ ->
test_bin(Atom, N-1)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment