The most basic way to measure function execution time is to use the function <tt>tc</tt> in the module <tt>timer</tt>. An example is shown below:

The most basic way to measure function execution time is to use the function <tt>tc</tt> in the module <tt>timer</tt>. An example is shown below:

−

<code>

+

<code caption="Timing a function with the timer module.">

1> timer:tc(lists, seq, [1,10]).

1> timer:tc(lists, seq, [1,10]).

{5,[1,2,3,4,5,6,7,8,9,10]}

{5,[1,2,3,4,5,6,7,8,9,10]}

Line 26:

Line 26:

The <tt>tc</tt> function is already quite useful, but generally function execution times vary depending on different circumstances. In most cases it is external ones, such as garbage collection, io operations etc. To get a more stable view of the performance of your function, a simple helper function is easy to write:

The <tt>tc</tt> function is already quite useful, but generally function execution times vary depending on different circumstances. In most cases it is external ones, such as garbage collection, io operations etc. To get a more stable view of the performance of your function, a simple helper function is easy to write:

−

<code>

+

<code caption="A function that measures execution time more elaborately, still using the timer module.">

test_avg(M, F, A, N) when N > 0 ->

test_avg(M, F, A, N) when N > 0 ->

L = test_loop(M, F, A, N, []),

L = test_loop(M, F, A, N, []),

Line 49:

Line 49:

With this function we get both the minimum, the maximum, the median and the average execution time:

With this function we get both the minimum, the maximum, the median and the average execution time:

Revision as of 08:29, 10 January 2008

Contents

Why?

Why would you want to measure the execution time of a function call? There could be several reasons:

You want to improve the speed of your code, measuring how much faster it is since the previous version

You want to compare two implementations of the same functionality with regards to speed

How

The Basic Way

The most basic way to measure function execution time is to use the function tc in the module timer. An example is shown below:

1> timer:tc(lists, seq, [1,10]).
{5,[1,2,3,4,5,6,7,8,9,10]}
2>

This tells us that generating the list with integers from 1 to 10 using the lists module took 5 microseconds (that's micro, not milli).

Making It More Useful

The tc function is already quite useful, but generally function execution times vary depending on different circumstances. In most cases it is external ones, such as garbage collection, io operations etc. To get a more stable view of the performance of your function, a simple helper function is easy to write: