Wednesday 23 October 2013

Tomorrow we begin with the joys of functions and sorting.  I remembered how slow the basic recursive fibonacci calculation could be and decide to compare Python to Julia.  Both are running on one of four CPUs and garbage collection is on so no cheating.  The code is:

Well, for fib(50) Julia took 107 seconds.  Seems slow until you find that Python took (actually I'm still waiting and it started before I wrote the Julia code) ... ... 

I also started learning a bit about Julia's Glamor of Graphics style plotting module, Gadfly.  Here's todays experiment:


Still waiting for Python to finish ... perhaps after dinner ... 1 hour 10 minutes and 5 seconds.  So Julia is 39 times faster than Python calculating the fib.

3 comments:

  1. There is a typo in your Julia code: the function is called "fib2" but it calls "fib".

    ReplyDelete
  2. Also, it would be better to do:

    fib(n) = return(n > 1 ? (fib(n-1) + fib(n-2)) : one(n))

    where you use one(n) to ensure that the result has the same type as n.

    ReplyDelete
  3. Correct about the typo -- too many experiments leaving names lying around.

    I like the type definition, thanks, and have amended it. I should probably limit it to integers as well. I'm currently struggling with scopes in memoization so I'll probably come back and improve it. No, it was easy. Done.

    ReplyDelete