# [erlang-questions] Execution delays

Jared Kofron <>
Thu Aug 9 16:17:28 CEST 2012

```Hi Ashley,
You might give fprof a whirl (http://www.erlang.org/doc/man/fprof.html).

JK

On Thu, Aug 9, 2012 at 6:45 AM, Ashley Holman <> wrote:

> Argh, silly me!  I think I just figured out what's happening.  It's the
> final multiplication in fac_multi_collect/2 that's having to run after the
> third process returns.  It is multiplying some pretty massive numbers so
> that would explain it.
>
> This raises a more general question though - are there some profiling
> tools out there so that I can more easily find out where the time is going
> and when things are executing?
>
> Thanks very much.
>
>
> On Thu, Aug 9, 2012 at 11:02 PM, Ashley Holman <> wrote:
>
>> 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
>>
>
>
> _______________________________________________
> erlang-questions mailing list
>
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120809/13f411b6/attachment.html>
```