Python vs Rust vs C++ Speed Comparison

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

КОМЕНТАРІ • 133

  • @TommyLeeBlaze
    @TommyLeeBlaze 2 місяці тому +346

    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  2 місяці тому +25

      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 Місяць тому +9

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

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

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

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

      It depends on the vary of applications thus we chose the correct language! Thanks for your cool video!

  • @wassim-akkari
    @wassim-akkari 2 місяці тому +214

    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  2 місяці тому +23

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

    • @wassim-akkari
      @wassim-akkari 2 місяці тому +8

      @@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 Місяць тому

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

  • @softwaremkxvii
    @softwaremkxvii 2 місяці тому +34

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

  • @dyn01234567
    @dyn01234567 Місяць тому +172

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

    • @Ezequiel-Lima
      @Ezequiel-Lima Місяць тому +4

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

    • @Crecross
      @Crecross Місяць тому +5

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

    • @MrNatsuDragneel
      @MrNatsuDragneel Місяць тому +5

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

    • @raulguerreroflores1460
      @raulguerreroflores1460 Місяць тому +6

      C++ is the best of all times

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

      phyton and java are for dummies

  • @АндрейЧерныш-з3у
    @АндрейЧерныш-з3у Місяць тому +36

    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.

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

    This video has no use, you can't compare Rust when it's not in release mode ...

  • @Micha-cc8hf
    @Micha-cc8hf Місяць тому +3

    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

  • @johnnybueti
    @johnnybueti 2 місяці тому +24

    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 Місяць тому +2

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

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

    Didn't know I was messing around with such a fast language

  • @Ekalai
    @Ekalai 2 місяці тому +5

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

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

      Ok can you further explain this?

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

    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.

  • @stasuchuvadov
    @stasuchuvadov 12 днів тому +1

    Unoptimized speed comparsion? Why?

  • @hishalvallabh8721
    @hishalvallabh8721 3 місяці тому +31

    rust release mode?

    • @97wowplayer
      @97wowplayer  3 місяці тому +17

      nope, all 3 are on debug

    • @cn-ml
      @cn-ml 3 місяці тому +14

      ​@@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  3 місяці тому +8

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

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

      ​@@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  3 місяці тому +1

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

  • @alexandergascon911
    @alexandergascon911 2 місяці тому +43

    C++ Rules!

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

    could you also test Mojo?

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

      Sure
      What should I compare it with?

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

      @97wowplayer I'd say C++ and Rust

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

    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.

  • @urparvezali
    @urparvezali 2 місяці тому +12

    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 Місяць тому

      blazing fast🚀🚀🚀🚀😎

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

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

    • @nilimdaimari
      @nilimdaimari Місяць тому +3

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

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

    why n/2 ? should be sqrt(n)

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

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

    • @jan-u7g4r
      @jan-u7g4r 21 день тому

      sqrt is expensive lol it should be *0.5 its faster than divide

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

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

  • @andreasalvi6422
    @andreasalvi6422 2 місяці тому +14

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

    • @ShidNoh
      @ShidNoh Місяць тому +6

      and?

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

      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 Місяць тому +2

      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 Місяць тому +1

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

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

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

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

    You only need to iterate up to sqrt(n) (inclusive) for the prime check algorithm.

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

    What abaout php?)

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

      3 times faster than Python.

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

    Naah, slowness. C++ /w SSE intrinsics should be here as well, possibly C++ /w DXC but that is an overkill (huge time to code, which is not a problem as everyone also has their own wrappers for the longer stuff, and bullet-fast execution).

  • @udaythakur7822
    @udaythakur7822 20 днів тому

    can someone suggest where to practice c++ for begineers

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

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

  • @ukrainian-playlists
    @ukrainian-playlists Місяць тому +2

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

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

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

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

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

    • @ukrainian-playlists
      @ukrainian-playlists Місяць тому

      @@97wowplayer Hi, thanks!

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

    What are using IDEs?

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

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

  • @Perceptron-bz4nl
    @Perceptron-bz4nl 26 днів тому

    can you try this in assembly

    • @97wowplayer
      @97wowplayer  25 днів тому +1

      @@Perceptron-bz4nl I will.
      Do you want me to compare all 4 languages or just assembly with one of these?

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

    cpp flexes with its speed

  • @CarlosCosta-cz3hb
    @CarlosCosta-cz3hb Місяць тому

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

  • @Rubin-Igor
    @Rubin-Igor Місяць тому +1

    in rust build --release

  • @LithiumDeuteride-6
    @LithiumDeuteride-6 Місяць тому

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

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

      We have std::print now. Though if you test on godbolt this bumps up compile times a bit compared to printf

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

    python
    for i in range(1, 100001):

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

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

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

      Depends on how often you need to run the code. Are your users waiting longer?

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

    Develop by python
    Transfer C by AI
    Run in C

  • @소금-h6k
    @소금-h6k Місяць тому

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

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

    But compare the code consumption in the programming language..

  • @mo4tal
    @mo4tal 2 дні тому

    C++ propaganda (I just love python and this is a meme, please respectable c++ developers; don't beat me up...)

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

    I choose c#

  • @francis.joseph
    @francis.joseph 27 днів тому

    I have tested the code in python ,its executing less than 1 s

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

    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.

  • @cheesepop7175
    @cheesepop7175 23 дні тому

    How is rust even slower than C++? They're both compiled languages.

    • @homomorphic
      @homomorphic 23 дні тому

      Although the above isn't a good example, in the real world, with expertly written c++ code, the compiler can do far more precomputation at compile time.

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

      rust compiled in debug

  • @ingmanuellucena3123
    @ingmanuellucena3123 20 годин тому

    Ok aunque tardas más escribiendo es más rápido corriendo imaginó porque es un lenguaje de bajo nivel y se ejecuta más rápido en el procesador felicidades

  •  2 дні тому

    kaka just for fun.

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

    C?

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

    Because iam python dev that's wy python is winner 😁

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

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

  • @TheIndieGhost
    @TheIndieGhost 26 днів тому

    Fun fact: you can make this faster for all three languages by only checking up to sqrt(n).

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

    Python with regex:
    import re
    def is_prime(n):
    return not re.match(r'^.?$|^(..+?)\1+$', '1'*n)
    print(is_prime(8))

  • @niteshroyal30
    @niteshroyal30 18 днів тому

    C++ is the best

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

    and at runtime? hehe

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

    rust without iterators and debug mode :(

  • @alexjrmatta
    @alexjrmatta 26 днів тому

    Inicio la guerra de los lenguajes 😂😂😂, hay al menos 6000 lenguajes y toda la vida salen con estos temas, que va a morir c++ o Python y que ahora es Rust, 😂😂😂

  • @helenmachelen4200
    @helenmachelen4200 23 дні тому

    Well, rust tried

  • @Eddie.Mootsen
    @Eddie.Mootsen 21 день тому

    Fortran 😉

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

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

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

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

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

    Cpp like so easy.

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

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

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

    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 2 місяці тому +2

      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.

  • @CarlosCosta-cz3hb
    @CarlosCosta-cz3hb Місяць тому

    Se calhar são 2 mais engano

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

    bro its not about how fast the output is , its actually about how easy it is to learn and do python ....... remember ~!

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

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

  • @unzerror8208
    @unzerror8208 23 дні тому

    Спс кэп

  • @kriz5652
    @kriz5652 26 днів тому

    If two languages has the same features to use the underlying hardware like multi core, multithreading programming usage comparing languages in regard of Speed is bullshit, it depends on the programmer and the compiler or interpreter. The computer does not RUN Phyton or c or c++ code it only runs machine code. Please author dont waste your time and our time with this kind of crap

  • @ukrainetoday960
    @ukrainetoday960 2 місяці тому

    Compare with vlang)

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

      thanks for the suggestion, I will look into it

    • @onyvid9557
      @onyvid9557 2 місяці тому

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

    • @ukrainetoday960
      @ukrainetoday960 2 місяці тому

      @@onyvid9557 😂

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

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

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

      You don’t have to watch

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

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

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

      Point taken

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

    C++ is a lenguage very ugly.

  • @vit.c.195
    @vit.c.195 26 днів тому

    You should be mega lamer to make binary on rust working slower that on C++. First next time make program on VIM and tests on Linux and yeah... publish -march parameters.