[erlang-questions] string to binary

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Thu Oct 20 00:55:41 CEST 2011


On Wed, Oct 19, 2011 at 16:36, Wes James <comptekki@REDACTED> wrote:
> This is interesting. I run statistics() four times and on line 2 the
> line is slower than line 4, but then after that it is always a little
> faster.  What does reduction mean?  Number of internal operations?
> Why would it be slower at first then faster the rest of the time?

Erlang/OTP contains a reduction counter for each process. This counter
is normally increased by one for each function or BIF-call that
process makes. After 2000 of such reductions, a context switch is made
to another process see erl_vm.h CONTEXT_REDS.

Your measurement has two problems. For one, these reductions is a
rather bad measure of the actual time spent. It is way better to use
the call timer:tc/2 for these kinds of small performance things. While
"expensive" things will cost more than a single reduction and GC also
costs reductions, it is a rather crude measure. For the Erlang VM it
is a simple way to know when to change context, but thats about it.

The second problem is more subtle: The Erlang shell is interpreted and
not compiled into bytecode and then executed. Thus, it is not uncommon
to see the shell being 5 times slower than a compiled module. To get
accurate measurement, you should place all of your functions in a
module, only export the relevant test functions and then use
timer:tc/2 to test these. It gives a better view of how fast a given
solution is.

-- 
J.



More information about the erlang-questions mailing list