Python vs C++ vs Java (Speed Comparison)

Sdílet
Vložit
  • čas přidán 21. 07. 2023
  • 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.com/file/d/1kzk_...

Komentáře • 97

  • @petemartinez3017
    @petemartinez3017 Před 9 měsíci +115

    Python is good for scripting and readability but not performance.

    • @gostan2718
      @gostan2718 Před 9 měsíci +24

      I find it hard to read Python

    • @I_am_Raziel
      @I_am_Raziel Před 8 měsíci +4

      Pure Python, yes. But there are ways and tricks to speed it up. A lot.

    • @doublekamui
      @doublekamui Před 7 měsíci

      ​@@I_am_Raziel​​do you mean by writing the part that need high performance in c++?

    • @I_am_Raziel
      @I_am_Raziel Před 7 měsíci +4

      @yetanothergamedevchannel Me, too. I miss the C- style curly brackets.

    • @revimfadli4666
      @revimfadli4666 Před 6 měsíci

      ​@@I_am_Razielespecially the bunch of compiled libs like numpy

  • @BlueIsLeet
    @BlueIsLeet Před 5 měsíci +40

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

    • @alessandrorossi1294
      @alessandrorossi1294 Před měsícem

      Yeah this test doesn't account for JVM warmup time and then counts the entirety of python interpreter warmup time

  • @Chris-ty7fw
    @Chris-ty7fw Před 8 měsíci +40

    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.

    • @shadowofheaven3279
      @shadowofheaven3279 Před 7 měsíci +1

      Correct

    • @screenworks267
      @screenworks267  Před 7 měsíci +2

      that makes a lot of sense, thanks for the info

    • @aniketbisht2823
      @aniketbisht2823 Před 6 měsíci +5

      "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.

    • @shadowofheaven3279
      @shadowofheaven3279 Před 6 měsíci +1

      ​@@aniketbisht2823 Read the original comment again

    • @Chris-ty7fw
      @Chris-ty7fw Před 6 měsíci

      @@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.

  • @simoncowell1029
    @simoncowell1029 Před 9 měsíci +20

    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 ?

    • @rudrakshmishra2761
      @rudrakshmishra2761 Před 7 měsíci +6

      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.

    • @BlueIsLeet
      @BlueIsLeet Před 5 měsíci +1

      too many factors that go into compile time for C programs to know

    • @bradleywang289
      @bradleywang289 Před 5 měsíci +1

      C is the fastest

    • @yancgc5098
      @yancgc5098 Před měsícem

      @@bradleywang289 Assembly is theoretically faster than C

  • @vinothm8726
    @vinothm8726 Před 11 měsíci +5

    Super music and comparison

  • @user-xc1rl4nc4q
    @user-xc1rl4nc4q Před 10 měsíci +53

    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

    • @NikGlt
      @NikGlt Před 9 měsíci +3

      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)

    • @User-je7gf
      @User-je7gf Před 8 měsíci +6

      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

  • @MikeM-ps1kn
    @MikeM-ps1kn Před 4 měsíci

    @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++

  • @mattiaslaserskold137
    @mattiaslaserskold137 Před 7 měsíci +1

    I like the music :P

  • @being_aslam_tiger
    @being_aslam_tiger Před rokem +7

    Compare it with C# 11

  • @Timely-ud4rm
    @Timely-ud4rm Před 5 měsíci +2

    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++

    • @hadrian2801
      @hadrian2801 Před 5 měsíci

      Rust

    • @kenarnarayaka
      @kenarnarayaka Před 4 měsíci +1

      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

  • @ninadmahalle1647
    @ninadmahalle1647 Před 26 dny

    Did you optimize the code According to language

  • @DinHamburg
    @DinHamburg Před 8 měsíci +1

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

  • @NewLondonMarshall
    @NewLondonMarshall Před 2 měsíci

    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

  • @sahazanomenarazafimandimby734

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

  • @andreduartebueno
    @andreduartebueno Před měsícem

    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.

  • @user-vh4mf4ux2m
    @user-vh4mf4ux2m Před měsícem

    What about c#?

  • @MegaDestrok
    @MegaDestrok Před 11 dny

    JS - what is compilation ?

  • @Pyovali
    @Pyovali Před 9 měsíci +8

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

    • @getforfree8344
      @getforfree8344 Před 8 měsíci +3

      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

    • @rowsncols
      @rowsncols Před 8 měsíci

      ah yes the low level language C@@getforfree8344

    • @itsentdev
      @itsentdev Před 8 měsíci

      @@getforfree8344 C++ can do drivers. A lot of Windows components are written in C++.

    • @shadowofheaven3279
      @shadowofheaven3279 Před 7 měsíci +2

      I have no idea whether this is satire, because you sound pretty serious

    • @SussyBaka-il1ku
      @SussyBaka-il1ku Před 6 měsíci

      @@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.

  • @Thisisdcode
    @Thisisdcode Před 5 měsíci +2

    hey atleast c++ was consistent

  • @Swyateg
    @Swyateg Před 9 měsíci +16

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

    • @oktay9784
      @oktay9784 Před 9 měsíci +17

      It also has the most secure logging tool called 'log4j' lol

    • @hba6018
      @hba6018 Před 9 měsíci +3

      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.

    • @goblinjedly9528
      @goblinjedly9528 Před 9 měsíci

      @@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

    • @oktay9784
      @oktay9784 Před 9 měsíci

      @@goblinjedly9528 keep calm bro Im just joking

    • @goblinjedly9528
      @goblinjedly9528 Před 9 měsíci

      @@oktay9784 Still dont undrestand why would anyone use logging lib

  • @VulgoGS
    @VulgoGS Před 7 dny

    C++ is HUGE!

  • @dan-tv1kp
    @dan-tv1kp Před 8 měsíci +1

    Shoulda used GraalVM

  • @valdissmith8592
    @valdissmith8592 Před 13 dny

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

  • @jamestk656
    @jamestk656 Před 5 měsíci

    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.

    • @Anubis1101
      @Anubis1101 Před 5 měsíci

      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.

  • @phononmaser1024
    @phononmaser1024 Před 7 měsíci +2

    JavaはJVMの起動にも時間がかかるからこう言う様なちょっとしたコードを実行するには部が悪すぎる

  • @viewererdos
    @viewererdos Před 8 měsíci +9

    There is nothing better than C/C++.

    • @geekygymrat
      @geekygymrat Před 8 měsíci +9

      Assembly :)

    • @viewererdos
      @viewererdos Před 8 měsíci

      @@geekygymrat These C languages are “high-level” assembly languages.

    • @calming_nothing
      @calming_nothing Před 8 měsíci

      ​@@geekygymrat binar code)))))

    • @keeIie
      @keeIie Před 7 měsíci +1

      @@geekygymrat still not better?

    • @doublekamui
      @doublekamui Před 7 měsíci +2

      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.

  • @gizlischone2289
    @gizlischone2289 Před 3 měsíci

    it means python isn't a turtle

  • @jorge.barcelos
    @jorge.barcelos Před 6 měsíci

    Python has no JIT bro

  • @glassfish7207
    @glassfish7207 Před 8 měsíci

    com

  • @unbreakablefootage
    @unbreakablefootage Před 8 měsíci

    ....

  • @duvaljoannaprisca7856
    @duvaljoannaprisca7856 Před 5 měsíci

    😂🎉😢😢

  • @DavidKujo
    @DavidKujo Před 3 měsíci

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

    • @JoseLucasd
      @JoseLucasd Před 2 měsíci

      Lol yes. Having no low level understanding really is a problem (this isnt for the video).

  • @aniketbisht2823
    @aniketbisht2823 Před 6 měsíci +2

    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.

    • @MikeM-ps1kn
      @MikeM-ps1kn Před 4 měsíci

      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.

    • @Bob-1802
      @Bob-1802 Před měsícem

      If Java is slow to compile, Kotlin (based in Java) compile time is horrendously slow. Almost an order of magnitude slower 🥱😴

  • @Fillrate
    @Fillrate Před 5 měsíci

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

  • @tzimisce1753
    @tzimisce1753 Před 4 měsíci

    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++.

    • @yancgc5098
      @yancgc5098 Před měsícem +1

      Cython is not faster than C++ bruh 😂

    • @tzimisce1753
      @tzimisce1753 Před měsícem

      @@yancgc5098 Measure it yourself if you don't believe me.

  • @duvaljoannaprisca7856
    @duvaljoannaprisca7856 Před 5 měsíci

    😂🎉😢😢