Note that there are some explanatory texts on larger screens.

plurals
  1. POFSharp runs my algorithm slower than Python
    primarykey
    data
    text
    <p>Years ago, I solved a problem via dynamic programming:</p> <p><a href="https://www.thanassis.space/fillupDVD.html" rel="noreferrer">https://www.thanassis.space/fillupDVD.html</a></p> <p>The solution was coded in Python.</p> <p>As part of expanding my horizons, I recently started learning OCaml/F#. What better way to test the waters, than by doing a direct port of the imperative code I wrote in Python to F# - and start from there, moving in steps towards a functional programming solution.</p> <p>The results of this first, direct port... are disconcerting:</p> <p>Under Python:</p> <pre><code> bash$ time python fitToSize.py .... real 0m1.482s user 0m1.413s sys 0m0.067s </code></pre> <p>Under FSharp:</p> <pre><code> bash$ time mono ./fitToSize.exe .... real 0m2.235s user 0m2.427s sys 0m0.063s </code></pre> <p>(in case you noticed the "mono" above: I tested under Windows as well, with Visual Studio - same speed).</p> <p>I am... puzzled, to say the least. Python runs code faster than F# ? A compiled binary, using the .NET runtime, runs SLOWER than Python's interpreted code?!?!</p> <p>I know about startup costs of VMs (mono in this case) and how JITs improve things for languages like Python, but still... I expected a speedup, not a slowdown!</p> <p>Have I done something wrong, perhaps?</p> <p>I have uploaded the code here:</p> <p><a href="https://www.thanassis.space/fsharp.slower.than.python.tar.gz" rel="noreferrer">https://www.thanassis.space/fsharp.slower.than.python.tar.gz</a></p> <p>Note that the F# code is more or less a direct, line-by-line translation of the Python code.</p> <p>P.S. There are of course other gains, e.g. the static type safety offered by F# - but if the resulting speed of an imperative algorithm is worse under F# ... I am disappointed, to say the least.</p> <p><strong>EDIT</strong>: Direct access, as requested in the comments:</p> <p>the Python code: <a href="https://gist.github.com/950697" rel="noreferrer">https://gist.github.com/950697</a></p> <p>the FSharp code: <a href="https://gist.github.com/950699" rel="noreferrer">https://gist.github.com/950699</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload