%% Author: blaxter %% Created: 13/07/2008 %% Description: Benchmark is a module for helping you to do benchmark stuff -module(benchmark). -export([do/1, do_silent/1]). -import(timer, [now_diff/2]). %% %% Executes a function an evaluates the time it costs %% do(F) -> Time_in_ms = do_silent(F), benchmark_message(Time_in_ms). %% %% Executes a function an returns the time it costs %% do_silent(F) -> Before = erlang:now(), F(), timer:now_diff(erlang:now(), Before). %% %% Private functions %% benchmark_message(N) when N < 1000 -> io:format("Execute time: ~p microseconds~n", [ N ]); benchmark_message(N) when N < 1000000 -> io:format("Execute time: ~p ms~n", [ N/1000 ]); benchmark_message(N) when N < 60000000 -> io:format("Execute time: ~p s~n", [ N / 1000000 ]); benchmark_message(N) -> io:format("Execute time: ~p min ~p seg~n", [ N div 60000000, (N rem 60000000) div 1000000 ]).