Python vs C++ vs Java (Speed Comparison)

preview_player
Показать описание
this is a speed test between java, c++ and python. the code might not be perfect so please let suggest any improvements in the comments.

All the code used in one .txt file:
Рекомендации по теме
Комментарии
Автор

Python is good for scripting and readability but not performance.

petemartinez
Автор

The test doesn't account for JVM warm up time.

BlueIsLeet
Автор

My 2, 300 - line C program compiles in 2 seconds using GCC via Codeblocks, on a laptop that was a mid-range personal use product 6 years ago. Why is it so fast, compared with this little program here ?

simoncowell
Автор

The test has an issue as it handicaps java's slow start up time ...

Java has a horrible start up time as it optimises as it runs, with C++ you provide those optimsations at the start and they are fixed. It will compile its compiled byte code down to assembler only after so many iterations. You can turn those opts off and let the JIT just kick .
Basically run the Java a lot of times on the same problem (without restart) and it will speed up exponentially when the JIT kicks after a number (configurable) of runs in as your then just running assembler like the compiled C++ which it's written in.

So on a high speed trading server (trading in micros, timed in nanos) you'd warm Java up before you start trading (a known drawback of java). C++ starts exponentially faster but doesn't learn as it goes on run it in five years it's still optimized as it was.
You wouldn't use Python at all in this scenario ie anything real time or multithreaded but its very good and just doing general stuff ;-)

You pays your money you takes yours choice and for the record I can program and use all three.

Chris-tyfw
Автор

Try in cases where you put big arrays like 1million with big amount of cicles like 10k i then you will see that java is fast as c lang, i did test already, using mergeSort algorithm

xcrlncq
Автор

Did you optimize the code According to language

ninadmahalle
Автор

Can you do c++ vs Rust? I wanna know which coding language is the fastest and most optimize. Ie meaning it is very optimize to utilize all resource starting up as fast as it can. I think I know the answer C++ but I wanna know if something beats C++

Timely-udrm
Автор

@Screen Works: A suggestion to improve the reliability of the comparison: Increase the amount of samples by several orders of magnitude. Currently, the results are prone to suffering from jitter, as the CPU load on a system varies. Secondly, run the code multiple times in a row and keep the lowest result. C++ is compiled AOT where as the JVM uses JIT. In its current form, the test only tells you about the performance of the first run. Although some commentators here enjoy ranting about the JVM/JIT, it optimizes the code quite aggressively up to the point where it can outperform C++ in certain cases like this simple bubble sort algorithm. That's another story for complex programs, of course, garbage collection takes its toll and cannot compete with optimized memory management in C++

MikeM-pskn
Автор

i have never sorted an array with 7 entries.. - and i have sorted *many* arrays...

DinHamburg
Автор

Python simplifies the programmer's life a little and drastically worsens the user's life; Python consumes 4x more memory than C++ and is 60x slower in the tests we perform here with various codes.
Python for those who are not computer scientists even makes sense, to run administrative problems and simple things, prototypes;
But for computer scientists I don't see the point.
The great advantage of programming languages ​​is to speed up the USER's life, saving time to carry out their activities. C/C++ does this, optimizing memory and processing time in favor of the user and the environment.
Python makes it easier for the programmer and slower for users...

Another point, a program is compiled a few times until we reach the final version. And it will be used thousands of times by thousands of users, so compilation time is only relevant to those who program.
Once again, attention should be paid to the objective of computer science, which should be to make things easier for the user and not for the programmer.
First the customer's interest.
Second, maintain the profession at a high level.

andreduartebueno
Автор

Guess nVidia should write their drivers in Python from now on lol

Pyovali
Автор

Super, merci pour la comparaison, Cependant je pensais que java est plus rapide que c sharp

sahazanomenarazafimandimby
Автор

Java is not for speed from start. Java is for creating secured soft

Swyateg
Автор

I love how python wasn't even compiled and it took more than 1/3rd the time to 'compile' than C++. What a slow language

NewLondonMarshall
Автор

and repeat the same, but with 10000+ online users, interacting with the system, please

valdissmith
Автор

For the applications I make, language execution speed pales in comparison to database access over a network lol. If anything, I'd be much more interested in memory consumption of each since that's usually the constraint I run into the most.

jamestk
Автор

Java, slow to compile and slow to run. The fuck do you get. But there are these Java people out there brainwashed about how good their JVM/JIT is, "once JIT kicks in, then ....".
If you care about performance, you don't rely on some mystical tooling, you check and analyse the assembly that has been generated and make best use of the low level primitives, that the hardware and the operating system exposes, for your specific use case and of course you measure the performance.
With Java you get minimal control over these low level details and need to rely on the almighty JVM.

aniketbisht
Автор

The problem for most Python programmers is that they don't know how the language works. Your test is simply wrong.

DavidKujo
Автор

I tried it myself and found that C++ resulted in being twice as slow as Java😅

Fillrate
Автор

1 second = nanoseconds.
Python has PyPy now, which is incredibly fast, but the only drawback is that it can't support every module/library because it doesn't support C properly.
PyPy used to have the drawback of not having support for the data science modules, but it does now in 2024.
And Cython can be used to make Python faster than C++.

tzimisce
welcome to shbcf.ru