News for hackers. Stuff that matters.
Re:Python Is Not C by bite_victim on Thursday July 02, 2015 @12:20PM (#9819895)
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

Re:Python Is Not C by maxerickson on Thursday July 02, 2015 @12:28PM (#9819947)
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

Re:Python Is Not C by bite_victim on Thursday July 02, 2015 @12:49PM (#9820064)
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.
Re:Python Is Not C by maxerickson on Thursday July 02, 2015 @12:53PM (#9820092)
The call to int is unnecessary when using the built in.
Re:Python Is Not C by bite_victim on Thursday July 02, 2015 @01:06PM (#9820175)
I am a clumsy individual. Dropping the int call resulted in shaving 3.5 seconds. But how is this possible?! It's ridiculous, really!
Re:Python Is Not C by maxerickson on Thursday July 02, 2015 @01:33PM (#9820337)
The CPython interpreter is pretty naive, so it more or less does what you tell it to.
Re:Python Is Not C by bite_victim on Thursday July 02, 2015 @01:40PM (#9820379)
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...

Re:Python Is Not C by acveilleux on Thursday July 02, 2015 @12:50PM (#9820068)
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
Re:Python Is Not C by fsk on Friday July 03, 2015 @12:32PM (#9826511)
What compiler settings were you using? In my experience, C is faster than Java if you turn on full optimization settings.
Re:Python Is Not C by bite_victim on Friday July 03, 2015 @01:37PM (#9826869)
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.

Re:Python Is Not C by fsk on Friday July 03, 2015 @02:31PM (#9827169)
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.

Re:Python Is Not C by bite_victim on Friday July 03, 2015 @02:48PM (#9827265)
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!

Re:Python Is Not C by fsk on Friday July 03, 2015 @03:59PM (#9827627)
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

Re:Python Is Not C by acveilleux on Thursday July 02, 2015 @12:53PM (#9820095)
Java's JIT probably generated pretty close to the same assembly code as the C version. Hence the "same" result.