Created
October 23, 2012 08:39
-
-
Save motiejus/3937666 to your computer and use it in GitHub Desktop.
list and binary copy benchmark in Erlang
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
test: t.beam | |
erl -eval 't:bench(), init:stop()' | |
t.beam: t.erl | |
erlc -smp +native $< | |
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
-module(t). | |
-export([bench/0, test_bin_copy/1, test_string_copy/1]). | |
bench() -> | |
bench(1 bsl 20, 30). | |
bench(N, Rep) -> | |
Self = self(), | |
io:format("Creating str of size ~p.. ", [N]), | |
L = [X rem 255 || X <- lists:seq(1, N)], | |
io:format("done.~n"), | |
io:format("Converting to binary.. "), | |
B = iolist_to_binary(L), | |
io:format("done.~n"), | |
io:format("Executing PREFILL BIN copy ~p times... ", [Rep]), | |
spawn_link(fun() -> bench_str(B, Rep, Self) end), | |
_TimesBinX = receive {bin, A2} -> A2 end, | |
io:format("done.~n"), | |
timer:sleep(100), | |
io:format("Executing PREFILL STR copy ~p times... ", [Rep]), | |
spawn_link(fun() -> bench_bin(L, Rep, Self) end), | |
_TimesStr = receive {str, A0} -> A0 end, | |
io:format("done.~n"), | |
timer:sleep(100), | |
io:format("Executing STR copy ~p times... ", [Rep]), | |
spawn_link(fun() -> bench_bin(L, Rep, Self) end), | |
TimesStr = receive {str, A1} -> A1 end, | |
io:format("done.~n"), | |
timer:sleep(100), | |
io:format("Executing BIN copy ~p times... ", [Rep]), | |
spawn_link(fun() -> bench_str(B, Rep, Self) end), | |
TimesBin = receive {bin, A3} -> A3 end, | |
io:format("done.~n"), | |
timer:sleep(100), | |
MeanBin = lists:sum(TimesBin) / Rep, | |
MeanStr = lists:sum(TimesStr) / Rep, | |
StdBin = std_dev(TimesBin, MeanBin), | |
StdStr = std_dev(TimesStr, MeanStr), | |
io:format("BIN: Min: ~9.1f, Max: ~9.1f, Mean: ~9.1f, stdev: ~8.1f~n", | |
[float(lists:min(TimesBin)), float(lists:max(TimesBin)), | |
MeanBin, StdBin]), | |
io:format("STR: Min: ~9.1f, Max: ~9.1f, Mean: ~9.1f, stdev: ~8.1f~n", | |
[float(lists:min(TimesStr)), float(lists:max(TimesStr)), | |
MeanStr, StdStr]). | |
bench_bin(L, Rep, Pid) -> | |
TimesStr = [ | |
begin | |
{T, _} = timer:tc(fun() -> test_string_copy(L) end), | |
T | |
end || _ <- lists:seq(1, Rep) | |
], | |
Pid ! {str, TimesStr}. | |
bench_str(B, Rep, Pid) -> | |
TimesBin = [ | |
begin | |
{T, _} = timer:tc(fun() -> test_bin_copy(B) end), | |
T | |
end || _ <- lists:seq(1, Rep) | |
], | |
Pid ! {bin, TimesBin}. | |
test_bin_copy(Bin) -> | |
test_bin_copy(Bin, <<>>). | |
test_bin_copy(<<>>, Accum) -> | |
Accum; | |
test_bin_copy(<<Char, Rest/binary>>, Accum) -> | |
test_bin_copy(Rest, <<Accum/binary, Char>>). | |
test_string_copy(Bin) -> | |
test_string_copy(Bin, []). | |
test_string_copy([], Accum) -> | |
lists:reverse(Accum); | |
test_string_copy([Char|Rest], Accum) -> | |
test_string_copy(Rest, [Char|Accum]). | |
std_dev(Values, Avg) -> | |
Sums = lists:foldl( | |
fun(V, Acc) -> D = V - Avg, Acc + (D * D) end, | |
0, Values), | |
math:sqrt(Sums / (length(Values) - 1)). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment