Python vs C++ vs Java (Speed Comparison)
HTML-код
- Опубликовано: 25 авг 2024
- 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:
drive.google.c...
Python is good for scripting and readability but not performance.
I find it hard to read Python
Pure Python, yes. But there are ways and tricks to speed it up. A lot.
@@I_am_Razieldo you mean by writing the part that need high performance in c++?
@yetanothergamedevchannel Me, too. I miss the C- style curly brackets.
@@I_am_Razielespecially the bunch of compiled libs like numpy
The test doesn't account for JVM warm up time.
Yeah this test doesn't account for JVM warmup time and then counts the entirety of python interpreter warmup time
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.
Correct
that makes a lot of sense, thanks for the info
"C++ doesn't learn as it goes", well compilers keep getting new optimizations. Also the C++ compilers have more knowledge of the whole program at compile time to make better decisions especially if you use header-only libraries and templates. And for optimizing on some data, there's a thing called profile guided optimization.
And if you really care about latency then you would look at the assembly generated for the specific code path you're concern with (with tools like Compiler Explorer) and not rely on the almighty JVM.
@@aniketbisht2823 Read the original comment again
@@aniketbisht2823
"well compilers keep getting new optimizations" - they do and the C++ applications Java compiler and Java virtual machine make use of these easily by moving up java versions but I meant in this case while the code runs, it can/will compile to assember just in time and profile itself against a real run. With java you could profile against some data but it can also profile and adjust itself as it runs as well. The "almighty JVM" by the way is a very nicely written C++ application you should look at the code it's really nice.
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 ?
C is like dressing up Assembly in a tuxedo. There's not much hidden in C when compared to other languages. (Think of format specifiers and how no other language really needs them but C does). It's also blazzingly fast because it communicates to the hardware directly without spending much time abstracting stuff.
too many factors that go into compile time for C programs to know
C is the fastest
@@bradleywang289 Assembly is theoretically faster than C
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
Idk why but after learning data structures, feels like merge sort algorithm is the most weird and for some reason uncomfortable to write (by write I mean you are given a few image examples of how the sort works but you need to think of the code yourself, radix is child's play for me in comparison to merge sort)
The reason why java is so slow here is because it has to initialize the JVM (Java virtual machine) only then can it start running the code
Where as C++ can start running immediately
Compare it with C# 11
Super music and comparison
@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++
Java is not for speed from start. Java is for creating secured soft
It also has the most secure logging tool called 'log4j' lol
In fact, it is one of the fastest running languages that exist, thanks to its just-in-time compiler, its optimization and techniques for generating optimized code according to the architecture where the program is being executed. Many people have very old references about Java (like your comment) and have no idea how fast this language is.
@@oktay9784 Its made by apache. WHAT DO U EXPECT? U dont want to send emails using logging lib? U are wierd everyone uses that. And also they are using deprecated stuff so ye good job apache
@@goblinjedly9528 keep calm bro Im just joking
@@oktay9784 Still dont undrestand why would anyone use logging lib
I was thinking Java was very mutch more slow than C++...
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++
Rust
Rust often is faster, because unless you find a ton of fast performance libraries, C++ tends to be slower. Rust is also memory-safe, and is way better for web development. The standard rust library is also way faster than C++ standard library, but people have written faster C++ standard libraries like EA Standard Library
But have fun fighting with the compiler for 7 hours straight lmao
hey atleast c++ was consistent
i have never sorted an array with 7 entries.. - and i have sorted *many* arrays...
i loved the background music. it was like sitting in a piano lounge watching it all unfold on a big screen w a scotch on the rocks.
Guess nVidia should write their drivers in Python from now on lol
Python Cant achieve it, its out of the capability of python, infact even out of capability of C++, for low level tasks such as parts of drivers directly interacting with machines low level languages are used which include Assembly or C
ah yes the low level language C@@getforfree8344
@@getforfree8344 C++ can do drivers. A lot of Windows components are written in C++.
I have no idea whether this is satire, because you sound pretty serious
@@getforfree8344 C++ is literally just C (+ predefined functionalities), and is able to do anything C is able to. though, with the tradeoff of compile time and performance.
Did you optimize the code According to language
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
what
@@xirate7091what
JS - what is compilation ?
There is nothing better than C/C++.
Assembly :)
@@geekygymrat These C languages are “high-level” assembly languages.
@@geekygymrat binar code)))))
@@geekygymrat still not better?
maybe rust, it's design is good too for avoid memory leak when programmer forget to delete variable or other human error, and zig but this is too new so less library.
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.
What about c#?
and repeat the same, but with 10000+ online users, interacting with the system, please
I like the music :P
it means python isn't a turtle
Shoulda used GraalVM
Super, merci pour la comparaison, Cependant je pensais que java est plus rapide que c sharp
C++ is HUGE!
Python has no JIT bro
The problem for most Python programmers is that they don't know how the language works. Your test is simply wrong.
Lol yes. Having no low level understanding really is a problem (this isnt for the video).
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.
thats why there are so many different languages, and the argument over whats best is ever ongoing.
you dont know for sure whats best for you until you test it yourself.
JavaはJVMの起動にも時間がかかるからこう言う様なちょっとしたコードを実行するには部が悪すぎる
com
....
I tried it myself and found that C++ resulted in being twice as slow as Java😅
how tf is that even possible.
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.
You don't seem to get the point. The level of optimization you're talking about is not feasible in most cases. Just run the examples yourself and get your facts straight. I've slightly modified this video's example: The array of numbers to be sorted is 5000 and in every language 1000 iterations are done, the lowest time is used to compare performance. Result: C++ ( g++ -Ofast -O3 -march=native) uses double the time as the OpenJDK 17 build, python is 100x slower, even. Try to optimize that with pure C++, in the end you'll resort to C with inline assembly to beat the JIT compiler's output. And then you've just optimized for one platform.
If Java is slow to compile, Kotlin (based in Java) compile time is horrendously slow. Almost an order of magnitude slower 🥱😴
😂🎉😢😢
1 second = 1000000000 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++.
Cython is not faster than C++ bruh 😂
@@yancgc5098 Measure it yourself if you don't believe me.
😂🎉😢😢