Python Speed Comparison: Faster Than We Thought?!

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

КОМЕНТАРІ • 105

  • @1Richmar
    @1Richmar Рік тому +15

    With the -Ox compilation option enabled in C++, the code would execute instantly (close to zero) since the compiler would recognize that the variable "sum" is never accessed and has no side effects. Consequently, your code would simply become "int main() {//nothing here}". If your code did access "sum," for example, by printing it out using "cout

  • @tips1483
    @tips1483 Рік тому +27

    You should also mention that other implementations are faster then others, some like PyPy which has a just in time compiler option can optimize code a crazy amount.

  • @NewLondonMarshall
    @NewLondonMarshall Рік тому +7

    This test wasnt fair for Java and Go. Go had the build included in the time when it could have just been running the binary, and Java was not compiled to a .class file so of course it would be much slower in interpreted mode.

  • @veroxsity2336
    @veroxsity2336 Рік тому +41

    i swear this guy never checks comments and is literally never online in his discord at all. its like he makes a video then goes to narnia

    • @chriskeo392
      @chriskeo392 Рік тому +8

      Yea I don't understand that. The whole point is to get subscribers right? 😂
      Communicating effectively with comments gets you subs.
      He needs to watch tutorials on YouTubing effectively

    • @none_ghost6204
      @none_ghost6204 Рік тому +8

      He is probably on the grind when he goes off camera

    • @sadf1416
      @sadf1416 Рік тому +6

      Who cares? This dude arguably has among the best python videos on the internet. This dude could easily combing cash in at a FANG job but instead provides these videos to us.

    • @veroxsity2336
      @veroxsity2336 Рік тому

      @@sadf1416 yea I understand that but usually when you do tutorials, you should at least see if anyone has problems. That's the whole point of teaching.

    • @lilDaveist
      @lilDaveist Рік тому

      @@sadf1416 no he couldn’t

  • @leopoldtiotsop
    @leopoldtiotsop Рік тому +7

    It is true that python is slower at execution, but to have high performance, it is often recommended to use built-in functions (programmed in C) like sum in this case. With sum(range(100000000)), the speed will be increased.

    • @tielessin
      @tielessin Рік тому +1

      I just tried this on Colab and this simple change actually makes the code run 7x faster. It went from taking 15.061 seconds to only 2.147 seconds.

    • @Oeuvre-Bramon
      @Oeuvre-Bramon 10 місяців тому +1

      So just use c

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

      To sum a range you can use a formula with O(1) i think it is (n² + n) / 2

  • @AbhishekBM
    @AbhishekBM Рік тому +16

    You should've done go build and then execute the binary produced. That would've been faster

  • @kamalshkeir
    @kamalshkeir Рік тому +7

    the node js loop benchmark one errored, we didn't get the real result

  • @Cave_Dev
    @Cave_Dev Рік тому +14

    Why are you not compiling the Java code via "javac Main.java" first and after that invoking the interpreter with "java Main"? By simply using "java Main.java" you are actually measuring compile time for generating the byte code plus execution time (you don't do that for your C code).

    • @nocopyrightgameplaystockvi231
      @nocopyrightgameplaystockvi231 Рік тому

      Because C compiles to binaries directly. Java is Code to class then to binary.

    • @dr.rainereschrich1549
      @dr.rainereschrich1549 Рік тому +2

      @@nocopyrightgameplaystockvi231 you could use GraalVM native image to also compile to native code.
      Generally I think benchmarking such low code makes no sence. it give you only a hint of trivial functions. To really do benchmarking you have to go through multiple different processing areas (compute, memory management, IO etc.) and also let all of them run longer times. Else you can hit a "sweet spot" in one of the languages. Also if you compare pre-compiled, runtime languages and just in time compiled you need to at least run them all longer to avoid differences in startup time.

    • @nomadvagabond1263
      @nomadvagabond1263 Рік тому

      Bcz he suck he didnt compile go either lol

    • @nocopyrightgameplaystockvi231
      @nocopyrightgameplaystockvi231 Рік тому

      @@dr.rainereschrich1549 agreed. Although for me compilation never mattered at all, unless you are running something incredibly intensive like over 2M appends to a list, which I once tested myself using Python's List, Numpy arrays and Array types. Guess who was the winner. I actually like more of Data structure based speed tests where Python falls behind most languages unless you start substituting with Cython.

    • @blindshellvideos
      @blindshellvideos Рік тому +2

      because the guy who posts videos on this channel has no idea what he is doing

  • @caju84
    @caju84 Рік тому +3

    Javascript code was not executed actually. Because you've typed time node.main.js ...and got 'Command not found' error.

  • @mikejohnston9113
    @mikejohnston9113 Рік тому +3

    On my computer I get similar results for the python and C matrix time measurements. If I use -O3 to compile the C program it is 4 times faster than the python version

  • @Zwiesel66
    @Zwiesel66 Рік тому +12

    Would be interesting to see how fast it is with Python 3.11.

    • @electra2707_
      @electra2707_ Рік тому +6

      3.12 its out with 60% more speed

    • @outofahat9363
      @outofahat9363 Рік тому +2

      @@electra2707_ Python 3.11 is 60% faster than 3.10. Python 3.12 is not out yet

  • @SkyFly19853
    @SkyFly19853 Рік тому +3

    I hope you also talk about Cython, CPython, and other extensions for speed comparison.

  • @rons96
    @rons96 Рік тому

    Very nice video, thank you, and this operator you publish before as the most unknown is really awesome.

  • @camsaffari
    @camsaffari 11 місяців тому +1

    You mistakenly ran "time node.main.js" instead of "time node main.js". The speed for JS was wrong. Also, Python generates bytecodes. You need to rerun the code to measure the real speed.

  • @nocopyrightgameplaystockvi231
    @nocopyrightgameplaystockvi231 Рік тому +6

    3.11 is faster than previous python versions.

  • @falklumo
    @falklumo Рік тому +1

    IMHO, a few things need clarification:
    - The java times include compilation time, unlike C. Also, java gets faster when executing a function more than once, possibly faster than C. Also, javascript gets faster.
    - Like numpy, BLAS libraries can also be used with C or Java.
    - The python syntax is more beginner-friendly but bites back in large projects. Like lack of encapsulation etc.
    Otherwise, good video with some valid points.

    • @nocopyrightgameplaystockvi231
      @nocopyrightgameplaystockvi231 Рік тому

      I don't think you can actually write java bytecode directly

    • @falklumo
      @falklumo Рік тому +1

      @@nocopyrightgameplaystockvi231 javac compiles java, just like gcc compiles C.

    • @falklumo
      @falklumo Рік тому +2

      @@nocopyrightgameplaystockvi231 My java timings, compared to NeuralNine's java command =100%) are:
      - 24.5% (javac; time java)
      - 11.6% (10 iterations to give the hotjava compiler a chance, 10.4% after 100 iterations - C does not get faster when executed repeatedly)
      As the C version in NeuralNine's test looks like being 2x as fast as Java (or is it 4x?), the true performance is the same for C and Java, if not Java is faster.
      My original Java timing (w/o javac, 1 iteration) is:
      real 0m0.299s
      user 0m0.306s
      sys 0m0.037s
      which is faster than C. This is in line with the common understanding that compiled java and C are about equally fast.

    • @nocopyrightgameplaystockvi231
      @nocopyrightgameplaystockvi231 Рік тому

      @@falklumo It does. But the end product of Javac isn't binaries as GCC does. Its a class file which has to be processed again by the interpreter for creating MC and then you can see the output.

    • @falklumo
      @falklumo Рік тому +2

      @@nocopyrightgameplaystockvi231 Actually, javac compiles to a binary just like gcc does. The difference is that the binary code is for a virtual processor, not an X86 or ARM. (Sun Microsystems actually built actual hardware processors once upon a time ;) ) But there should be little to no performance difference for long enough an execution. The technology isn't too different from what Apple does for the M1 with X86 binaries actually. You wouldn't say X86 code on the M1 is interpreted because it is blazingly fast and actually converted into ARM code on the fly ...

  • @thatmagicalcat
    @thatmagicalcat Рік тому +1

    add a `-O3` while compiling c code and see the results

  • @sunkanmiotitolaye9354
    @sunkanmiotitolaye9354 Рік тому +2

    How did you customize the windows and Linux terminals with the background images?

  • @grzegorzryznar5101
    @grzegorzryznar5101 Рік тому +1

    There will be one more player in this game. Mojo. Could you make some video about it (with benchmarking)?

  • @dookshi
    @dookshi Рік тому +1

    I'm all for Python as a quick devel tool but your comparison to C, unfortunately, is not fair and is even somewhat misleading. At around 14:38 you should change the order of loop nesting from i, j, k to i, k, j and only then report the time. How much faster is it now?

  • @aquaeyed82
    @aquaeyed82 Рік тому

    wow, thanks for sharing knowledge, subscibed.

  • @alphabasic1759
    @alphabasic1759 Рік тому +1

    Completely disagree with the development speed comment. That should be an entirely new metric of productivity. Calling it speed just confuses.

  • @Radical9535
    @Radical9535 Рік тому +1

    interesting but did you take into consideration that it may be different process times with bigger programs.? Also, optimization largely depends on how the code is written. optimization is basically the least amount of readable code in a program, as I understand it.

    • @lengors7327
      @lengors7327 Рік тому

      "Optimization is the least amount of readable code in a program" - what? Are you talking about optimizing developement speed? Because otherwise this makes no sense

    • @Radical9535
      @Radical9535 Рік тому +1

      @@lengors7327 yes development speed.

  • @ChimeraGilbert
    @ChimeraGilbert Рік тому

    I just want to add a few things. If you use PyPy intead of CPython for the code in this video, you'll find that it's actually faster than the C program. But if you used a more optimized algorithm for each of these programs, the C is much faster. The C code would be much faster if you write it like this
    #include
    int main() {
    long long N = 100000000;
    long long sum = (N * (N - 1)) / 2;
    printf("Sum: %lld
    ", sum);
    return 0;
    }

  • @Volker-Dirr
    @Volker-Dirr 10 місяців тому

    hmm... The last variant is unfair, since you use an external library to do the multiplication. You can do that in (nearly) every other language also and then the other language is also faster in developing/coding.

  • @lixiang7349
    @lixiang7349 Рік тому +1

    Why don’t you use numpy for python?

  • @qbaliu6462
    @qbaliu6462 3 місяці тому

    I think you forgot to recompile the updated go script.

  • @anamoyeee
    @anamoyeee Рік тому

    Why did it take so long to not find a command in bash ("node." instead of "node ")

  • @NishithSavla
    @NishithSavla Рік тому +1

    You should have used hyperfine to benchmark all these

  • @CASnumber
    @CASnumber Рік тому

    Bixly interests me thank you

  • @LithiumDeuteride-6
    @LithiumDeuteride-6 Рік тому

    Faster than assembly language! And at the expense of python and the simplicity of the code. This can also be done in assembler, and the code will be just as simple. You just need to write a macro that implements such a function, it will take some time, but then everything will be as simple as on Python.

    • @LithiumDeuteride-6
      @LithiumDeuteride-6 Рік тому

      In assembler, I replaced the FPU code with SSE2, and this added speed, it was 460 msec, it became 314 msec.

    • @LithiumDeuteride-6
      @LithiumDeuteride-6 Рік тому

      This is the code for UASM!
      ;; v0.0
      .686
      .xmm
      .model flat, stdcall
      option casemap:none
      include \masm32\include\windows.inc
      include \masm32\include\kernel32.inc
      includelib \masm32\lib\kernel32.lib
      include msvcrt.inc
      include macros.asm
      .code
      main proc
      local time:sdword, tmp:sdword, i:sdword, j:sdword
      local A:dword, B:dword, R:dword
      mov time, clock()
      ASSUME esi:ptr real8, edi:ptr real8, ebx:ptr real8
      mov A, GlobalAlloc(GPTR, 500*500*8)
      mov B, GlobalAlloc(GPTR, 500*500*8)
      mov R, GlobalAlloc(GPTR, 500*500*8)
      mov esi, A
      mov edi, B
      mov ebx, R
      .for (eax=0, i=eax: i

    • @LithiumDeuteride-6
      @LithiumDeuteride-6 Рік тому

      It turned out to speed up the code, after all, the assembler is faster than Python, even if some particularly magical libraries are used in this python of yours.
      mov ebx, R
      .for (eax=0: eax

  • @serkan_bayram
    @serkan_bayram Рік тому

    Would you consider local pricing on your books in the future?

  • @alphabasic1759
    @alphabasic1759 Рік тому

    Why is it that you aren’t initializing the C array in the python using numpy like you are for the Java and the c?

  • @ayoubelmhamdi7920
    @ayoubelmhamdi7920 Рік тому

    in c , you are use double instead of int or uint16

  • @alphabasic1759
    @alphabasic1759 Рік тому

    It wasn’t necessary to use the includes in the C programs.

  • @lilcwn
    @lilcwn Рік тому

    is the @ in python really using that "k" loop too? just curious

  • @muralir8504
    @muralir8504 Рік тому

    can we compare same with 3.9 vs 3.11 i think there should be huge difference

  • @HardlineCheats
    @HardlineCheats Рік тому

    you don't ran the node script there was a error

  • @Volker-Dirr
    @Volker-Dirr 10 місяців тому

    Why did you code the c code so complicated? Line 1 and 2 are not needed.

  • @joelmartinez7326
    @joelmartinez7326 Рік тому

    ¿Por qué mejor no lo pruebas con un algoritmo y de ordenamiento o búsqueda?

  • @dipeshsamrawat7957
    @dipeshsamrawat7957 7 місяців тому

    Excellent! ❤

  • @woody5012
    @woody5012 Рік тому

    You should've built the go binary first... surprised you're not aware of this... "go run" first has to build, then run - so it'll be slower.

  • @xbwafi
    @xbwafi Рік тому

    dose vim works in pycharm???

  • @betapacket
    @betapacket Рік тому

    9:46 💀bro really did node.main.js LOL

  • @Miracle-uc6es
    @Miracle-uc6es Рік тому

    I understand your points. However, this is Python's fault.
    My opinion is Python should give us to choose of assigning static variables and compile the code.
    The default settings may be dynamic data types and interpreter, I have no problem with that, but that is the exact reason of why Python is slow.
    As a user I would like to have an option to write code like C but in Python syntax (if I need speed), If I do not, then I could write code as the default Python settings. But that should be my choice.
    for example, age is an integer value, the interpreter should not "have to" check in every single time. (By the way it is has to be a positive integer value between (0, 127), if I write that in C, I defined it as char age; So, more optimizations can be made)
    I have no idea about why they do not let us define any static value, static data type and compile my code (Interpreter great while developing but after final product developed, I prefer a compiled program.
    Like.
    const PI = 3.14
    int age = 20

    • @demolazer
      @demolazer Рік тому

      Cython does precisely this without having to learn a huge stack of C syntax.

  • @goncaloazevedo9822
    @goncaloazevedo9822 Рік тому

    The first task wasn't run for javascript, it failed and you only assessed the time it took to fail. Also, java should be compiled first for a real measurement

  • @chriskeo392
    @chriskeo392 Рік тому

    I want python to one day be faster than C#

  • @RSP13
    @RSP13 5 місяців тому

    So, python is fast, as long as the code actually runs o C++.

  • @willyhorizont8672
    @willyhorizont8672 Рік тому

    I love py & js

  • @ulrich-tonmoy
    @ulrich-tonmoy Рік тому

    You should also add Rust and yea python is fast but not by default

  • @shanent5793
    @shanent5793 Рік тому

    One sample tells us less than nothing

  • @sbksdg7019
    @sbksdg7019 Рік тому

    wow, really amazing

  • @MirulFahimi
    @MirulFahimi Рік тому

    Python 3.11 very fast

  • @karmylr
    @karmylr Рік тому

    Full of loopholes

  • @consciousbeing7785
    @consciousbeing7785 Рік тому +1

    In java it takes just one line:
    IntStream.range(0, 1000000000).sum()

  • @veroxsity2336
    @veroxsity2336 Рік тому

    hey neural, theres an issue with your machine learning bot, we think the library is broken but not sure, please can you take a look

  • @lengors7327
    @lengors7327 Рік тому +1

    Is this guy an amateur pretending to be a professional? Either that or he's trying really hard to "sell" python. I see no other justification for such a terrible video.
    First and foremost, he doesnt write any "warmup" code that would allow some languages to have lower times on the benchmarks. He could then compare that to the version without said "warmup" code and see how large the difference is or isnt.
    Secondly, he does one run and thats it. No averages whatsoever. And to make matters worst, he's measuring compile times, for example, for java. Ridiculous honestly. He's also not using any optimizations for C which is just as ridiculous.
    Finally, I like how he tries to "sell" python at the end by comparing a version using a library to versions of Java and C without library. Yeah, python development speed is faster, but if you use Java and C libraries you can make those languages almost as fast development wise.
    Like c'mon man, at least put some effort into it.
    Edit: he also complete ignores different implementations of interpreters and compilers for each language which can produce very different results. What a joke of a video

  • @joelmartinez7326
    @joelmartinez7326 Рік тому

    R. no se escucha mucho como lenguaje de programación de alto nivel, pero me gusta su sintaxis.

  • @mechwarrior83
    @mechwarrior83 Рік тому

    Only mean to criticise constructively dude, your methodology is severely flawed. You are measuring the compile times along with the execution times. You should be compiling binaries and measuring execution speed of those.

    • @lilDaveist
      @lilDaveist Рік тому

      Never noticed his mistakes in previous videos?

  • @MrREALball
    @MrREALball Рік тому +1

    First of all, end-to-end measurement you are doing is completely incorrect. You should've done it inside of your scripts.
    Second, with python it is REALY hard to work on complex projects, since there is no type system (the one python has is a joke), hence the development speed would slow down dramatically as the project grows.
    Not hating on python, I think it is a great language for small projects and scripting.

    • @lilDaveist
      @lilDaveist Рік тому

      UA-cam was/is written in Python.

    • @demolazer
      @demolazer Рік тому

      You can create a virtual type system using MyPy. Google and Facebook even developed their own versions of it. It's not going to make execution any faster but it helps a lot in terms of development and working on complex projects.

  • @TalesFromTheSlumsOfMumbai
    @TalesFromTheSlumsOfMumbai Рік тому

    He’s definitely a Croat that’s living in Germany, looks like Mirko cro cop too

  • @SonusVolvebatur
    @SonusVolvebatur Рік тому

    n 1