Hi,<div><br></div><div>(code attached)<br><div><br></div><div>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).</div>
<div><br></div><div>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.</div>
<div><br></div><div>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. </div>
<div><br></div><div>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).</div><div><br></div><div>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.</div>
<div><br></div><div>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!</div>
<div><br></div><div>Thanks</div><div>Ash</div></div>