From 2cca6506bafedc5ebb720510151a98602133d8e5 Mon Sep 17 00:00:00 2001 From: Christopher Faulet Date: Tue, 6 Apr 2010 22:39:14 +0200 Subject: Add timer:tc/2 to measure the elapsed time of anonymous functions Works like timer:tc/3 but for anonymous functions. --- lib/stdlib/doc/src/timer.xml | 24 +++++++++++++++++++----- lib/stdlib/src/timer.erl | 13 ++++++++++++- lib/stdlib/test/timer_simple_SUITE.erl | 18 +++++++++++++----- 3 files changed, 44 insertions(+), 11 deletions(-) (limited to 'lib/stdlib') diff --git a/lib/stdlib/doc/src/timer.xml b/lib/stdlib/doc/src/timer.xml index 0b6807dd6c..3d54047e35 100644 --- a/lib/stdlib/doc/src/timer.xml +++ b/lib/stdlib/doc/src/timer.xml @@ -202,18 +202,32 @@ tc(Module, Function, Arguments) -> {Time, Value} - Measure the real time it takes to evaluate apply(Module, Function, Arguments) + tc(Fun, Arguments) -> {Time, Value} + Measure the real time it takes to evaluate apply(Module, + Function, Arguments) or apply(Fun, Arguments) Module = Function = atom() + Fun = fun() Arguments = [term()] Time = integer() in microseconds Value = term() -

Evaluates apply(Module, Function, Arguments) and measures - the elapsed real time. Returns {Time, Value}, where - Time is the elapsed real time in microseconds, - and Value is what is returned from the apply.

+

+ + tc/3 + +

Evaluates apply(Module, Function, Arguments) and measures + the elapsed real time. Returns {Time, Value}, where + Time is the elapsed real time in microseconds, + and Value is what is returned from the apply.

+
+ tc/2 + +

Evaluates apply(Fun, Arguments). Otherwise works + like tc/3.

+
+
diff --git a/lib/stdlib/src/timer.erl b/lib/stdlib/src/timer.erl index 36fdb48c75..12bbd45703 100644 --- a/lib/stdlib/src/timer.erl +++ b/lib/stdlib/src/timer.erl @@ -22,7 +22,7 @@ send_after/3, send_after/2, exit_after/3, exit_after/2, kill_after/2, kill_after/1, apply_interval/4, send_interval/3, send_interval/2, - cancel/1, sleep/1, tc/3, now_diff/2, + cancel/1, sleep/1, tc/2, tc/3, now_diff/2, seconds/1, minutes/1, hours/1, hms/3]). -export([start_link/0, start/0, @@ -98,6 +98,17 @@ sleep(T) -> after T -> ok end. + +%% +%% Measure the execution time (in microseconds) for Fun(Args). +%% +-spec tc(function(), [_]) -> {time(), term()}. +tc(F, A) -> + Before = erlang:now(), + Val = (catch apply(F, A)), + After = erlang:now(), + {now_diff(After, Before), Val}. + %% %% Measure the execution time (in microseconds) for an MFA. %% diff --git a/lib/stdlib/test/timer_simple_SUITE.erl b/lib/stdlib/test/timer_simple_SUITE.erl index 021a22c61b..6aa2b7b945 100644 --- a/lib/stdlib/test/timer_simple_SUITE.erl +++ b/lib/stdlib/test/timer_simple_SUITE.erl @@ -224,11 +224,19 @@ cancel2(Config) when is_list(Config) -> tc(doc) -> "Test sleep/1 and tc/3."; tc(suite) -> []; tc(Config) when is_list(Config) -> - % This should both sleep and tc - ?line {Res, ok} = timer:tc(timer, sleep, [500]), - ?line ok = if - Res < 500*1000 -> {too_early, Res}; % Too early - Res > 800*1000 -> {too_late, Res}; % Too much time + % This should both sleep and tc/3 + ?line {Res1, ok} = timer:tc(timer, sleep, [500]), + ?line ok = if + Res1 < 500*1000 -> {too_early, Res1}; % Too early + Res1 > 800*1000 -> {too_late, Res1}; % Too much time + true -> ok + end, + + % This should both sleep and tc/2 + ?line {Res2, ok} = timer:tc(fun(T) -> timer:sleep(T) end, [500]), + ?line ok = if + Res2 < 500*1000 -> {too_early, Res2}; % Too early + Res2 > 800*1000 -> {too_late, Res2}; % Too much time true -> ok end, -- cgit v1.2.3