[erlang-questions] Execution delays

Ashley Holman dscvlt@REDACTED
Thu Aug 9 15:32:56 CEST 2012


Hi,

(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!

Thanks
Ash
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120809/d4d1c298/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fac.erl
Type: application/octet-stream
Size: 2429 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120809/d4d1c298/attachment.obj>


More information about the erlang-questions mailing list