5 ms·
I hate to ruin if for you, but usually Python is only a few hundred times slower than C++, typical Java is maybe 20% slower, and well-written Java is about as f
by nine_k 12y ago
I hate to ruin if for you, but usually Python is only a few hundred times slower than C++, typical Java is maybe 20% slower, and well-written Java is about as fast.
- bjourne 12y agoNo. Here's some guys demonstrating 5000x+ speedup by rewriting Python code in C and C++: http://codegolf.stackexchange.com/questions/26323/how-slow-is-python-really-or-how-fast-is-your-language http://codegolf.stackexchange.com/questions/26323/how-slow-i... and http://codegolf.stackexchange.com/questions/26371/how-slow-is-python-really-part-ii http://codegolf.stackexchange.com/questions/26371/how-slow-i...
- nine_k 12y agoPython was sped up 30x, according to the same post, so the difference between best Python and best C++ becomes "mere" ~170 times. Of course, on numeric problems like this, plain CPython inevitably sucks performance-wise (no JIT, a lot of lookups, no value types).
- bjourne 12y agoAt least I linked some numbers. Your statement "usually Python is only a few hundred times slower than C++" is so vague it is completely useless. The Python code was sped up by replacing idiomatic Python with non-idiomatic code (not your "usual" Python) and also replacing a large part of it with calls to optimized code written in C. If your claim had been "Python is only a few hundred times slower if you outsource most of your work to C libraries" I would have had no objection. But that's not what you wrote.
- GFK_of_xmaspast 12y agoIt's simply not a fair comparison if you change the algorithms between implementations.
- olavk 12y agoThat is somewhat misleading, since the optimized versions use different algorithms. There is also an optimized Python version that runs 30 times faster. It is a combination of better algorithms and closer-to-the-metal language that gives the 5000x speedup described in the thread. You can get arbitrary large speedups by just choosing a worse initial algorithm and then improve it.
- YZF 12y agoThis is not an argument I can win, that is constructive or necessarily meaningful, but here's what happened when someone tried to translate C++ to an equivalent Java program: http://codegolf.stackexchange.com/a/26536 http://codegolf.stackexchange.com/a/26536 (x2.5) Also see: http://benchmarksgame.alioth.debian.org/u64/java.php http://benchmarksgame.alioth.debian.org/u64/java.php (median x2 but this is stuff people spent time on optimizing which is not really what we're talking about here) Java is not 20% slower or just as fast. But if it is please submit your Java solutions to the bench mark game for us to see. Python is a few hundred time slower as long as you restrict yourself to native types (e.g. strings, integers, dictionaries, lists). Once you start defining your own classes and do more sophisticated things you get a lot slower. At any rate, my point was not to pick some specific number, my point was that sometimes you just don't care at all about how fast it runs. The x10000 was from a real world system but this can vary - a lot.
- pron 12y agoIt's not hard to write single-threaded C/C++ code that's significantly faster than Java (although it will become harder once Java gets value types, that will address Java's biggest performance pitfall, namely cache-misses), and the simpler the code the better the chances for success (as complex code requires architecture employing virtual methods, and virtual method invocation in Java is faster than in C++ due to JIT inlining). But writing multithreaded code in C++ that's faster than Java is a lot harder, so much so that the C++ code will probably be significantly slower. The reason is that general-purpose lock-free data structures are a lot easier to write when you have a good GC (plus, the JDK includes state-of-the-art concurrent data structure implementations).
- igouy 12y ago>>but this is stuff people spent time on optimizing << Optimizing both Java and C++ programs.
- deleted 12y ago[deleted]
- tormeh 12y agoAbout optimization: If you really want to, the JVM can be surprisingly unrestrictive. You'd be amazed at the slightly mad things for example HFTs do with it. Advanced techniques include object reuse so you never activate the GC and unsafe calling of native code. Then there's the weird things you can do with the JVM directly. I can't find anything right now, but I recall weird annotations and multiline JVM arguments more wizardlike than anything. Finally there's alternative JVMs for those who want that.