Skip to content

Instantly share code, notes, and snippets.

@yfyf
Created July 21, 2013 17:22
Show Gist options
  • Save yfyf/6049220 to your computer and use it in GitHub Desktop.
Save yfyf/6049220 to your computer and use it in GitHub Desktop.
testing performance of reflection
-module(refl).
-compile(export_all).
normal(Foo) ->
lists:sort([Foo]).
bad(Mod, Fun, Foo) ->
Mod:Fun([Foo]).
bench(NumTests) ->
Seq = lists:seq(1, NumTests),
{FastTime, _} = timer:tc(fun lists:foreach/2,
[fun (_) -> normal(foo) end, Seq]),
timer:sleep(1000),
{SlowTime, _} = timer:tc(fun lists:foreach/2,
[fun (_) -> bad(lists, sort, foo) end, Seq]),
io:format("ResultFast: ~pms~n", [FastTime/1000]),
io:format("ResultSlow: ~pms~n", [SlowTime/1000]).
-----------------
Eshell V5.9.3 (abort with ^G)
1> refl:bench(1000).
ResultFast: 0.147ms
ResultSlow: 0.239ms
ok
2> refl:bench(1000*1000).
ResultFast: 42.222ms
ResultSlow: 73.34ms
ok
3> refl:bench(100*1000*1000).
ResultFast: 4661.811ms
ResultSlow: 6813.099ms
ok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment