diff --git a/lib/stdlib/doc/src/timer.xml b/lib/stdlib/doc/src/timer.xml index 2cbf7977a919..6f3aef8c7ec5 100644 --- a/lib/stdlib/doc/src/timer.xml +++ b/lib/stdlib/doc/src/timer.xml @@ -319,14 +319,37 @@ - - + + + Measure the real time it takes to evaluate Fun. + + + tc/3 + +

Calls function timer:tc(Module, Function, Arguments, microsecond).

+
+ tc/2 + +

Calls function timer:tc(Fun, Arguments, microsecond).

+
+ tc/1 + +

Calls function timer:tc(Fun, microsecond).

+
+
+
+
+ + + + + Measure the real time it takes to evaluate apply(Module, Function, Arguments) or apply(Fun, Arguments). - In microseconds + In the specified TimeUnit - tc/3 + tc/4

Evaluates apply(Module, Function, Arguments) and measures the elapsed real time as @@ -334,18 +357,18 @@ erlang:monotonic_time/0.

Returns {Time, Value}, where Time is the elapsed real time in - microseconds, and Value is what is + the specified TimeUnit, and Value is what is returned from the apply.

- tc/2 + tc/3

Evaluates apply(Fun, Arguments). - Otherwise the same as tc/3.

+ Otherwise the same as tc/4.

- tc/1 + tc/2

Evaluates Fun(). Otherwise the same as - tc/2.

+ tc/3.

diff --git a/lib/stdlib/src/timer.erl b/lib/stdlib/src/timer.erl index 780ae4c7f143..064f4c4fd999 100644 --- a/lib/stdlib/src/timer.erl +++ b/lib/stdlib/src/timer.erl @@ -24,7 +24,7 @@ exit_after/3, exit_after/2, kill_after/2, kill_after/1, apply_interval/4, apply_repeatedly/4, send_interval/3, send_interval/2, - cancel/1, sleep/1, tc/1, tc/2, tc/3, now_diff/2, + cancel/1, sleep/1, tc/1, tc/2, tc/3, tc/4, now_diff/2, seconds/1, minutes/1, hours/1, hms/3]). -export([start_link/0, start/0, @@ -247,41 +247,71 @@ sleep(T) -> Time :: integer(), Value :: term(). tc(F) -> + tc(F, microsecond). + +%% +%% Measure the execution time (in microseconds) for Fun(Args) +%% or the exeuction time (in TimeUnit) for Fun(). +%% +-spec tc(Fun, Arguments) -> {Time, Value} + when Fun :: function(), + Arguments :: [term()], + Time :: integer(), + Value :: term(); + (Fun, TimeUnit) -> {Time, Value} + when Fun :: function(), + TimeUnit :: erlang:time_unit(), + Time :: integer(), + Value :: term(). +tc(F, A) when is_list(A) -> + tc(F, A, microsecond); +tc(F, TimeUnit) -> T1 = erlang:monotonic_time(), Val = F(), T2 = erlang:monotonic_time(), - Time = erlang:convert_time_unit(T2 - T1, native, microsecond), + Time = erlang:convert_time_unit(T2 - T1, native, TimeUnit), {Time, Val}. %% -%% Measure the execution time (in microseconds) for Fun(Args). +%% Measure the execution time (in microseconds) for an MFA +%% or the execution time (in TimeUnit) for Fun(Args). %% --spec tc(Fun, Arguments) -> {Time, Value} +-spec tc(Module, Function, Arguments) -> {Time, Value} + when Module :: module(), + Function :: atom(), + Arguments :: [term()], + Time :: integer(), + Value :: term(); + (Fun, Arguments, TimeUnit) -> {Time, Value} when Fun :: function(), Arguments :: [term()], + TimeUnit :: erlang:time_unit(), Time :: integer(), Value :: term(). -tc(F, A) -> +tc(M, F, A) when is_list(A) -> + tc(M, F, A, microsecond); +tc(F, A, TimeUnit) -> T1 = erlang:monotonic_time(), Val = apply(F, A), T2 = erlang:monotonic_time(), - Time = erlang:convert_time_unit(T2 - T1, native, microsecond), + Time = erlang:convert_time_unit(T2 - T1, native, TimeUnit), {Time, Val}. %% -%% Measure the execution time (in microseconds) for an MFA. +%% Measure the exeuction time (in TimeUnit) for an MFA. %% --spec tc(Module, Function, Arguments) -> {Time, Value} +-spec tc(Module, Function, Arguments, TimeUnit) -> {Time, Value} when Module :: module(), Function :: atom(), Arguments :: [term()], + TimeUnit :: erlang:time_unit(), Time :: integer(), Value :: term(). -tc(M, F, A) -> +tc(M, F, A, TimeUnit) -> T1 = erlang:monotonic_time(), Val = apply(M, F, A), T2 = erlang:monotonic_time(), - Time = erlang:convert_time_unit(T2 - T1, native, microsecond), + Time = erlang:convert_time_unit(T2 - T1, native, TimeUnit), {Time, Val}. %% diff --git a/lib/stdlib/test/timer_simple_SUITE.erl b/lib/stdlib/test/timer_simple_SUITE.erl index 9031a2d242d9..761689dc5114 100644 --- a/lib/stdlib/test/timer_simple_SUITE.erl +++ b/lib/stdlib/test/timer_simple_SUITE.erl @@ -731,7 +731,19 @@ tc(Config) when is_list(Config) -> true -> ok end, + %% tc/4 + {Res4, ok} = timer:tc(timer, sleep, [500], millisecond), + ok = if + Res4 < 500 -> {too_early, Res4}; + Res4 > 800 -> {too_late, Res4}; + true -> ok + end, + %% Check that timer:tc don't catch errors + ok = try timer:tc(erlang, exit, [foo], second) + catch exit:foo -> ok + end, + ok = try timer:tc(erlang, exit, [foo]) catch exit:foo -> ok end, @@ -746,6 +758,7 @@ tc(Config) when is_list(Config) -> %% Check that return values are propageted Self = self(), + {_, Self} = timer:tc(erlang, self, [], second), {_, Self} = timer:tc(erlang, self, []), {_, Self} = timer:tc(fun(P) -> P end, [self()]), {_, Self} = timer:tc(fun() -> self() end),