Erlang/OTP Forums

Author Message

<  Open Telecom Platform (OTP)  ~  Estimate the time took to do something

delaw
Posted: Thu Aug 30, 2007 9:39 pm Reply with quote
Joined: 30 Aug 2007 Posts: 2
hello all,

First of all, please excuse me for my english :+)

I'm discovering the erlang language and OTP framework... it's great ! I like it ! It's fun :p

I would like to know how much time took to compute something in my program ?

I use the erlang:now() function before and after an operation, and i compute the difference between the two values.

I take the example from the book "programming erlang"

For example :

----------------
T1 = erlang:now(),
L = for(1,N,fun() -> spawn(fun() -> wait() end) end),
T2 = erlang:now(),
Diff = timer:now_diff(T2,T1),
--------------------------

The now_diff function return the time elapsed between T2 and T1.

But in the book "programmin erlang" (always the same, the bible ? ) the author used :

----------------
erlang:statistics(runtime),
erlang:statistics(wall_clock),
L = for(1,N,fun() -> spawn(fun() -> wait() end) end),
{_,Time1} = erlang:statistics(runtime),
{_,Time2} = erlang:statistics(wall_clock),
U1 = Time1 * 1000 / N,
U2 = Time2 * 1000 / N,
io:format("Process spawn time= ~p (~p) microseconds~n",[U1,U2]).
-----------------

The results are realy different if i use a method or the other.


Someone could explain me this difference ?


Thank you for your help.


delaw
View user's profile Send private message
michal
Posted: Fri Aug 31, 2007 5:33 pm Reply with quote
User Joined: 20 Jul 2006 Posts: 44 Location: London
Hi,
erlang:statistics/1 function returns time in milliseconds and then this value is multiplied by 1000 to get the time in microseconds. One of the reasons why you get different results could be related to this rounding.

erlang:statistics/1 function returns time since last call of this function with a particular parameter. What is the difference between runtime and wall_clock parameter? runtime indicates how long CPU was busy with your task only. If during execution of your task there were some other tasks that the CPU was busy with, then the wall_clock time will be higher than the runtime time. I would assume that wall_clock time is not very much different from what you get using erlang:now() function.

Michal

_________________
http://www.erlang-consulting.com
View user's profile Send private message
Mazen
Posted: Tue Sep 04, 2007 8:50 am Reply with quote
User Joined: 20 Jul 2006 Posts: 164 Location: London
Small tip:
If you want to know the time it took to run a function but also want to retrieve the result of that function in one go then consider using: timer:tc/3

It will return a tuple of {Time, Value} where the Time is the time it took to run in microseconds and Value is the return value of the function.

/Mazen

1) http://www.erlang.org/doc/man/timer.html
View user's profile Send private message
delaw
Posted: Sat Sep 08, 2007 9:13 am Reply with quote
Joined: 30 Aug 2007 Posts: 2
Thank you for your responses.


delaw
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum