Hacker News new | ask | show | jobs
by bite_victim 4000 days ago
Some time ago I wrote a really simple code snippet to see the performance differences between Python, PHP, C and Java (the languages I tinker in) on my particular machine (i3 M 330, 2.13 GHz / 4 GB RAM / Ubuntu 15.04 x64).

The results were as follows:

~ 14.2 seconds for Python 3.4.3 [1]

~ 9.0 seconds for Python 2.7.9 [1]

~ 9.0 seconds for PHP 5.6 [2]

~ 2.3 seconds for C [3]

~ 2.3 seconds for Java 8 [4]

Again, this was on my machine with out of the box settings. I have linked the test code that I wrote and perhaps there is something wrong with my Python and PHP code but to me the results were quite revealing. Also it's interesting to see that on my configuration C and Java both hit the limit of my CPU (I can't explain the score otherwise) and I can't know for sure if on a more powerful CPU Java would still be on pair with C.

[1] https://gist.github.com/anonymous/7edafa3889be967a1e1d

[2] https://gist.github.com/anonymous/56ff76849f5a312340d9

[3] https://gist.github.com/anonymous/5717ba935b43bad09e1d

[4] https://gist.github.com/anonymous/6b0c2f11609b951b64f3

3 comments

You should use the built in pow for the python version (or the double asterisk operator). I guess most of the difference with PHP is there.

The type conversion behavior of the math.pow function is clearly documented:

https://docs.python.org/3/library/math.html#math.pow

I have updated the test with the new figures that I got. Initially I only tested with Python 2 with math.pow. I have to say I am quite disappointed with the performance of Python 3 though and even using the built-in pow function, PHP 5.6 still is the fastest in this particular case.
The call to int is unnecessary when using the built in.
I am a clumsy individual. Dropping the int call resulted in shaving 3.5 seconds. But how is this possible?! It's ridiculous, really!
The CPython interpreter is pretty naive, so it more or less does what you tell it to.
PHP is no better, if you add an intval call it will add almost 4 seconds to the result! This is an extraordinary proof that being a little sloppy can cost you a lot when using interpreted scripting languages!

Also, I am thinking if and how much speed gain could one inject by not using OOP in PHP...

Indeed on my workstation (Xeon W3565 @ 3.2Ghz, x64 linux, Python 2.7):

    math.pow() version: 11.7s

    pow() version: 6.4s

    ** version: 5.2s
What compiler settings were you using? In my experience, C is faster than Java if you turn on full optimization settings.
Now that is just ridiculous! With O2 and O3 I get 0.03 seconds on average! I try to understand what could possibly mean all of the flags that get switched on with optimizations from this page: https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

I also wonder if Java has any kind of optimizations to experiment with.

If you don't put in ANY optimization flags, it puts in full debugging info, which slows things down a lot.

I don't know anything about Java optimization.

Java doesn't have any optimization flags and the debugging info slowdown makes sense I suppose. That is the exact difference between them, GCC with -O3 is almost 100x faster in another test I just did: 10s for Java, 0.1 for C. When you turn off the optimization flags C jumps right at the same 10s figure though.

Really cool interesting stuff!

That's what I figured - a lot of people who say "Java is as fast as C" aren't doing proper compiler optimizations on their C. I suspected that was true, but I didn't spend the time on a proper test.

-O2 or -O3 does some neat stuff like unrolling loops and inlining functions

--fast_math is another one I use

Java's JIT probably generated pretty close to the same assembly code as the C version. Hence the "same" result.