Skip to content

Instantly share code, notes, and snippets.

@rramsden
Created December 19, 2012 01:10
Show Gist options
  • Save rramsden/4333596 to your computer and use it in GitHub Desktop.
Save rramsden/4333596 to your computer and use it in GitHub Desktop.
quick benchmark of ets:lookup_element
% bench:start().
% {result1,57663,result2,43880}
-module(bench).
-export([start/0]).
-record(record, {
key,
one = 1.0,
two = 2.0,
three = 3.0,
four = 4.0
}).
-define(TIMES, 100000).
start() ->
ets:new(table, [named_table, {keypos, 2}]),
[ets:insert(table, #record{key=Key}) || Key <- lists:seq(1, ?TIMES)],
key = 7777,
fun1 = fun() -> [ets:lookup(table, Key) || _ <- lists:seq(1, ?TIMES)] end,
fun2 = fun() ->
Work = fun() ->
ets:lookup_element(table, Key, #record.one),
ets:lookup_element(table, Key, #record.two)
end,
[Work() || _ <- lists:seq(1, ?TIMES)]
end,
{T1, _} = timer:tc(Fun1),
{T2, _} = timer:tc(Fun2),
{result1, T1, result2, T2}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment