[erlang-questions] Execution delays

David Mercer <>
Thu Aug 9 15:38:11 CEST 2012

I did not look at your code, but given your description of the symptoms, and
that you have 2 cores working on 3 sublists, and that you observe one of
those cores pegging for a few seconds after the other one completes, one
hypothesis worth testing is that only 2 of the 3 sublists are complete and
that the third is still processing (on one core).


[mailto:] On Behalf Of Ashley Holman
Sent: Thursday, August 09, 2012 8:33 AM
Subject: [erlang-questions] Execution delays




(code attached)


I've just began learning Erlang over the last few days and so far a really
enjoyed programming in it.  As a learning exercise, I decided to implement
the classic "factorial" function: fac(N) = N*fac(N-1); F(0) = 1.  For
calculating large factorials, this takes a long time (fac(200000) takes 42
secs on my laptop).


So, I made some optimisations (using condense_terms/1 and shuffle/1 - but
you can basically ignore those).  However, it still was only using 100% of 1
cpu core, and I have 2 cores, so this was a good opportunity to learn an
important feature of Erlang, spawning processes!  I partitioned the list
into 3 sublists and spawned a process to multiply each sublist.  eg. [pseudo
code] Proc1([1,2,3]) * Proc2([4, 5, 6]) * Proc3(7, 8, 9) should give me the
result of fac(9) using 3 processes.


This appears to be working but I'm completely confused about certain delays
in execution I'm seeing - I'm not sure where it is spending some of its time
in the code.  I put in some debugging, and it appears that the child
processes (fac_multi_actor) are finishing their calculations and returning
the results, but the receiver (fac_multi_collect) doesn't return for several
seconds later.   


The whole point of this was to try to optimise it, so I would like to know
where these few seconds are going (~30% of execution time).


fac:fac(200000) is taking ~9.5s using the three processes, but based on the
debugging it looks like it should be completing in 6.  Something else hangs
around using 100% of one core for several seconds later.  I've attached the
code in case anyone is able to take a look at this behaviour.


PS. I realise it will take some effort for someone to actually bother
reading the code and trying to reproduce my problem, so no problem if I
don't get any answers.  In case anyone feels like helping though it would be
much appreciated because I'm a bit confused at the moment!




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120809/e70d5159/attachment.html>

More information about the erlang-questions mailing list