Python vs Rust vs C++ Speed Comparison

Поділитися
Вставка
  • Опубліковано 18 лис 2024

КОМЕНТАРІ • 90

  • @TommyLeeBlaze
    @TommyLeeBlaze Місяць тому +246

    Great post! Just to add on, as someone already mentioned, it's best to compare production-ready builds. On my machine, Rust in debug mode runs at 1.6s (similar to your results), but in release mode, it finishes in 0.2s-about 8x faster!

    • @97wowplayer
      @97wowplayer  Місяць тому +20

      Thank you, you can check if you're interested the release mode comparison video I made, however it's using a different algorithm for testing.

    • @xep14_88
      @xep14_88 21 день тому +3

      ​​@@97wowplayer you made the mistake, delete this video

    • @Tenebris_PR
      @Tenebris_PR 8 днів тому

      @@xep14_88 I tried the rust code with the release option and compiled the c++ code using the -o2 flag which enables speed optimizations. The result with the existing code was that the rust code was about 0.05 milliseconds faster. That said I changed all the ints to int32_t (which is the one that rust uses) in c++ and the speed differences were negligible. While testing the differences were linked more to the system's load at the moment than the code itself. I tested it with cl in windows and g++ in linux, same result on both. Maybe we should try to put the code on a loop for a million or more times and keep track of min/max/average execution time of each iteration.

  • @wassim-akkari
    @wassim-akkari Місяць тому +149

    rust debug mode is very slow because rust does far more checks in the run time, run rust with all optimizations enabled and in release mode then test the speed

    • @97wowplayer
      @97wowplayer  Місяць тому +15

      I have another video which compares release mode rust vs c++

    • @wassim-akkari
      @wassim-akkari Місяць тому +5

      @@97wowplayer I did watch it you used a different code there and not the same one here but I have tested that code locally as well and I have left a note under the video on my observations.

    • @Tenebris_PR
      @Tenebris_PR 8 днів тому

      @@wassim-akkari I tried the rust code with the release option and compiled the c++ code using the -o2 flag which enables speed optimizations. The result with the existing code was that the rust code was about 0.05 milliseconds faster. That said I changed all the ints to int32_t (which is the one that rust uses) in c++ and the speed differences were negligible. While testing the differences were linked more to the system's load at the moment than the code itself. I tested it with cl in windows and g++ in linux, same result on both. Maybe we should try to put the code on a loop for a million or more times and keep track of min/max/average execution time of each iteration.

  • @АндрейЧерныш-з3у
    @АндрейЧерныш-з3у 10 днів тому +10

    I tested the aglorhythm on PHP 8.3 on my laptop. In the range up to 100,000, the program execution time took 0.15 sec.
    In C++, a similar algorithm worked in 0.011 sec.

  • @dyn01234567
    @dyn01234567 23 дні тому +78

    How fast you can type out the code is a useless metric.

    • @Ezequiel-Lima
      @Ezequiel-Lima 22 дні тому +3

      É verdade para isso existe o copie e cole. A métrica está no tempo de execução.

    • @Crecross
      @Crecross 10 днів тому +2

      Not really if he's typing out at a constant speed

    • @MrNatsuDragneel
      @MrNatsuDragneel 9 днів тому

      More time is spent optimizing the code, business rules and refactoring than just typing the first version of the code.

    • @raulguerreroflores1460
      @raulguerreroflores1460 9 годин тому

      C++ is the best of all times

    • @raulguerreroflores1460
      @raulguerreroflores1460 9 годин тому

      phyton and java are for dummies

  • @softwaremkxvii
    @softwaremkxvii Місяць тому +7

    Learn C/CPP from top to bottom, from inner to out and you will solve 90% of the tasks.

  • @johnnybueti
    @johnnybueti Місяць тому +19

    Suggestion: to completely remove the impacts (if any) of time measurement across languages, I would measure the execution time in the shell instead.

    • @julyort6762
      @julyort6762 28 днів тому +2

      but python need to load much more things, is already slow yet

  • @Ekalai
    @Ekalai Місяць тому +4

    C++ performance depends on settings, optimization and compilers. If you twist the C++ ear or bite the tail, then see what happens?. 🌹🌹👌👌

    • @jersingh6837
      @jersingh6837 25 днів тому

      Ok can you further explain this?

  • @Micha-cc8hf
    @Micha-cc8hf 8 днів тому

    In cpp on M1 with -O3 level compilation the result is like
    > Primes: 9592
    > Elapsed time: 1.54812 milliseconds
    the typescript equivalent running by BunJS is
    > Primes: 9592
    > Elapsed time: 154.662083 milliseconds
    I guess TS result could be better if JIT would take an action but the code is not so heavy to involve it

  • @hishalvallabh8721
    @hishalvallabh8721 2 місяці тому +28

    rust release mode?

    • @97wowplayer
      @97wowplayer  2 місяці тому +13

      nope, all 3 are on debug

    • @cn-ml
      @cn-ml 2 місяці тому +12

      ​@@97wowplayernot sure if debug mode is a good idea for perf testing. Different compilers have different optimizations enabled by default. For a fair comparison imo, you have to give both compilers their fair chance by enabling all safe optimizations. Especially, because rust sometimes has quite some intensive compile time.

    • @97wowplayer
      @97wowplayer  2 місяці тому +8

      I'm planning on making a video with release mode and showing the speed differences

    • @ensuretime
      @ensuretime 2 місяці тому +3

      ​@@97wowplayer why are you comparing the speed of 3 languages in debug mode? the compiler adds a lot of garbage to make the code debuggable, this same code in c++ with optimizations enabled, the compiler would remove the loop and simply show the final result

    • @97wowplayer
      @97wowplayer  2 місяці тому +1

      @@ensuretime I am planning on making a release mode version to compare the 2 types of modes

  • @palborno
    @palborno 8 днів тому

    being c++ a stronger typed langague than the other two, your function isprime() should return 1or 0 instead of true or false being casted to int on return. this way you can add the results without any worries.

  • @CarlosCosta-cz3hb
    @CarlosCosta-cz3hb 5 годин тому

    Azar tens que querer três
    Estou a ver 3 quadros deveriam serem ovais ou redondos quais?

  • @gallanonim4259
    @gallanonim4259 Місяць тому +18

    why n/2 ? should be sqrt(n)

    • @hamzaiqbal7178
      @hamzaiqbal7178 26 днів тому +2

      n/2 is probably easier to understand for those who don't know about sqrt(n).

  • @urparvezali
    @urparvezali Місяць тому +7

    why not run cargo with --release flag?
    On my machine in debug mode it takes 2 seconds and in release mode it ran about 0.8 seconed. on the other hand C++ in -Ofast flag takes 0.851 second which is little slower than rust.

    • @Slavasil
      @Slavasil 24 дні тому

      blazing fast🚀🚀🚀🚀😎

    • @roshandev8167
      @roshandev8167 11 днів тому

      Thanks @urparvezali After watching this video i feel demotivated that again wasting my time in slow language like one more golang

    • @NilimCo
      @NilimCo 9 днів тому +2

      Do the same for C++ also in release mode and you will see the difference.

  • @andreasalvi6422
    @andreasalvi6422 29 днів тому +12

    What the hell kind of comparison are you making? Python is an interpreted language, while Rust and C++ are compiled languages!

    • @ShidNoh
      @ShidNoh 28 днів тому +3

      and?

    • @prawnydagrate
      @prawnydagrate 25 днів тому +2

      and how does that mean you can't compare them? ofc python will obviously be slower but that doesn't mean it's wrong to compare the languages

    • @AndrewI-n5l
      @AndrewI-n5l 24 дні тому +1

      It's wrong to compare languages because of implementation. I can write python code that runs faster than C code. Python is C under the hood, it's like comparing the mass of a car to the mass of its engine.

    • @sixtyonehack8460
      @sixtyonehack8460 22 дні тому +1

      @@AndrewI-n5l you mean you can not write phyton code that runs faster than c.

    • @music4life467
      @music4life467 11 днів тому

      python can be faster near to c if dont rely on pure python coding, use cython to optimize heavy computation

  • @SrAzion
    @SrAzion 5 днів тому +1

    0:13 Typing time is different from program running time. so it is useless to compare these two

  • @ukrainian-playlists
    @ukrainian-playlists 24 дні тому +2

    Hi! Enable multithreading in Python 3.13 and do a speed comparison Python 3.12 vs Python 3.13.

    • @97wowplayer
      @97wowplayer  24 дні тому

      Great idea!
      Will do it in the future! Thank you!

    • @97wowplayer
      @97wowplayer  21 день тому

      Hey, the requested video is up, you can check it out!

    • @ukrainian-playlists
      @ukrainian-playlists 19 днів тому

      @@97wowplayer Hi, thanks!

  • @yojaychang
    @yojaychang 3 дні тому

    Develop by python
    Transfer C by AI
    Run in C

  • @alexandergascon911
    @alexandergascon911 Місяць тому +35

    C++ Rules!

  • @veritasliberabitvos454
    @veritasliberabitvos454 25 днів тому

    Change algo to only count to sqrt the number, that is the highest factor. Save that to a variable for the check on the for loop. The compiler should optimise it, just double check.

  • @Rubin-Igor
    @Rubin-Igor 6 днів тому +1

    in rust build --release

  • @AndrewTalalai
    @AndrewTalalai 13 днів тому

    To see accurate results, try getting N from stdin. I think it’s obvious that C++ can't be slower!

  • @소금-h6k
    @소금-h6k 9 днів тому

    How about pypy? I know it is faster than python.

  • @LithiumDeuteride-6
    @LithiumDeuteride-6 23 дні тому

    The C++ code turned out to be cumbersome, it must be done in C, the printf function is simpler.

  • @nurlanalibayli8728
    @nurlanalibayli8728 21 день тому +1

    What abaout php?)

    • @ningauble
      @ningauble 12 днів тому

      3 times faster than Python.

  • @TabiG82
    @TabiG82 11 днів тому

    It's wrong result. Why did you use UNOPTIMALIZED builds for the speed test?

  • @housebba
    @housebba 13 днів тому

    python
    for i in range(1, 100001):

  • @programmingpillars6805
    @programmingpillars6805 21 день тому

    cpp flexes with its speed

  • @GuilhermeCassianoFerreiraSilva
    @GuilhermeCassianoFerreiraSilva 11 днів тому

    and at runtime? hehe

  • @viewererdos
    @viewererdos 27 днів тому

    C?

  • @aronlinesl
    @aronlinesl 17 днів тому

    What are using IDEs?

    • @97wowplayer
      @97wowplayer  17 днів тому

      CLion for C++, RustRover for Rust and PyCharm for Python

  • @tickshot
    @tickshot 22 дні тому

    Yup python takes least human time. Python is most performant.

  • @babananick9402
    @babananick9402 Місяць тому +1

    I just tried to do this in Kotlin for fun and I got...
    "Found 9592 primes in 0.841438700 seconds".
    Can anyone tell me why so? I did similar tests with Java/Kotlin/C++ about a year ago and found that in some cases C++ is even slower than Java or Kotlin. Is the speed of low-level languages ​​a conspiracy theory?

    • @soheilbeygi240
      @soheilbeygi240 28 днів тому +1

      Java/Kotlin/C# will all compile to machine code before the execution. Either by AOT or JIT. Technically they should be able to produce a performant machine code specific to the machine they are running on, well as in C++ case as far as I know that is not the case.
      Although I can't think of a case where either one would perform better than the other. For example if Intel releases a new CPU with some instruction for some kind of mathematical function C++/C#/Java compilers will be updated to support that particular instruction anyways. The difference is that you would have to recompile the C++ program with the new compiler.

  • @rsalmei
    @rsalmei 17 днів тому

    Nice, but you've lost the great opportunity of running Rust and C++ in release mode afterward, with all optimizations enabled, and actually comparing the speed of both compiled languages in this case.

  • @julyort6762
    @julyort6762 28 днів тому

    rust without iterators and debug mode :(

  • @engine0ficial
    @engine0ficial 5 днів тому

    I wanna be programmer ❤

  • @KiyopakaOfficial
    @KiyopakaOfficial 15 днів тому

    You forgot to plug in name space std; to shorten the program. C++
    Before space:
    Std::cout

  • @IorPerry
    @IorPerry Місяць тому +1

    I heard on the grapevine that pypy is faster, that its speed is comparable with c++

  • @Filimon_enc
    @Filimon_enc 10 днів тому

    Господи, чел сравнивает скорость написания кода

  • @UltimatusVirsus
    @UltimatusVirsus Місяць тому +2

    Man, i really hate the FAKE KEYBOARD SOUND, use instead some music idk but man, that sound is awful cringe

    • @joel9909
      @joel9909 27 днів тому

      You don’t have to watch

    • @UltimatusVirsus
      @UltimatusVirsus 27 днів тому

      @@joel9909 bro, if i put play on this video is because their main topic was interesting

    • @97wowplayer
      @97wowplayer  27 днів тому

      Point taken

  • @CarlosCosta-cz3hb
    @CarlosCosta-cz3hb 5 годин тому

    Se calhar são 2 mais engano

  • @denisg854
    @denisg854 10 днів тому

    с++.........UB

  • @leotravel85
    @leotravel85 21 день тому

    What a joke of a video, I thought they were about to compare the execution speed, who the f cares about this

  • @ukrainetoday960
    @ukrainetoday960 Місяць тому

    Compare with vlang)

    • @97wowplayer
      @97wowplayer  Місяць тому

      thanks for the suggestion, I will look into it

    • @onyvid9557
      @onyvid9557 Місяць тому

      vlang transpile to c. of course, faster then go/rust. 😂

    • @ukrainetoday960
      @ukrainetoday960 Місяць тому

      @@onyvid9557 😂