Cython makes Python INSANELY FAST

Поділитися
Вставка
  • Опубліковано 4 січ 2025

КОМЕНТАРІ • 54

  • @Carberra
    @Carberra  2 роки тому +19

    Thanks to Mitko for pointing this out - due to an oversight in my implementation, the benchmarks for the factorial function are actually incorrect. This doesn't degrade the educational value with regard to the syntax, which is all fine, but I thought I'd just mention it. Moral of the story: be careful around big numbers!

  • @MitkoNikov
    @MitkoNikov 2 роки тому +26

    Wait! You don't check if the answers are the same! I suspect if the code is converted to C/C++ without using Python's int object, it overflows and that's why it's way faster. It should be way faster eitherway, but does it properly use the python int object or just uses the primitive 4 bytes integer in C/C++?

    • @Carberra
      @Carberra  2 роки тому +16

      You are...right. Fuck, how did I not notice that?
      Thankfully I don't think it kills the educational value with regard to the syntax, but I will update the thumbnail to reflect the speedup from the sorting, which I've confirmed as legit.

  • @shner6742
    @shner6742 Рік тому +4

    Yo this is awesome bro, I run this code and it was actually 1867.571 times faster using Python 3.10.7 and C++ 14.0 (for the factorial() example)

  • @williammcenaney1331
    @williammcenaney1331 10 місяців тому +1

    You might write some Common Lisp params to benchmark them against Python programs solving the same problem. Common Lisp types dynamically. But some programmers wrote operating systems in it to run them on computers designed to run Lisp programs. Common Lisp lets you speed up your Lisp by statically typing some variables.

  • @divingradish
    @divingradish 2 роки тому +7

    "so" in "*.so" files stands for Shared Object.

    • @Carberra
      @Carberra  2 роки тому +2

      I see -- I had a hunch but wasn't 100% sure. Thanks for confirming (:

  • @traversis
    @traversis 2 роки тому +4

    Excellent video! New to Cython, but when I tried this without importing the cpython array it worked with similar speedups. Maybe they changed something in the last 6 months?

    • @Carberra
      @Carberra  2 роки тому +1

      Entirely possible. I haven't really gone back to this since the video so I couldn't tell you. It's possible the speed was roughly the same anyways.

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

    i'd assume the .so file is the same as every other .so file: a shared object. basically, a dynamically linked library.

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

    I wonder if PyPy could use some of its thorough data type analysis of Python code to convert Python to Cython. PyPy does JIT compilation but I wonder if mixing PyPy with Cython tools could lead to better performance.

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

    Step 1: ask ChatGPT generate simple python program for hard task
    Step 2: Cython generate C code
    Step 3: Ask ChatGPT to simplify each Cython code files
    Step 4: Go back to step 1

    • @hammerheadcorvette4
      @hammerheadcorvette4 9 місяців тому

      ChatGPT is not the end all be all. It's cool sure, it's not infallible.

  • @richardbennett4365
    @richardbennett4365 8 місяців тому

    Use deques (double-ended queues) instaed of lists in python if one wants fast execution times. It's why they are a part of python.

    • @Carberra
      @Carberra  8 місяців тому

      I recently made a video talking about this, if you're interested.

  • @RuslanKovtun
    @RuslanKovtun Рік тому +4

    You can't get 1000! fit in int64. Python's `int` contains all the digits, while C's `int` overflows silently.

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

      I thought all the cython stuff in the .so file makes it so the int allocate more space

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

    so, is a shared object, something like *.dll in Windows platforms...

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

    Great video!
    More videos on Cython please!
    In the video you mentioned the pure python mode didn't give the level of speedup of full Cython syntax...I experienced the same. Is this behaviour to be expected? I would rather write full Cython syntax if that's the case.

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

      Thank you! I believe so, as using Pure Python Mode requires the use of cpdef where cdef could be used in pure Cython. That's my understanding of it anyways, if I'm honest I haven't really done much with Cython since making this video!

  • @user-wr4yl7tx3w
    @user-wr4yl7tx3w 2 роки тому +2

    Can you call existing c math functions like matrix multiplication?

    • @Carberra
      @Carberra  2 роки тому

      Yep. The documentation shows how, but I've never really tried it.

  • @richardbennett4365
    @richardbennett4365 8 місяців тому

    Use numba in oython. Vastly simpler, and IT DOES GET ONE TO THE SPEEDS OF C.
    Also, there's pypy3 and codon, a language tgat uses python syntax, but is compiled, so wicked fast. 😮

    • @Carberra
      @Carberra  8 місяців тому

      I've made videos on both Numba and PyPy, if you're interested.

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

    100,000! doesn’t blow up the max int value for C? That seems suspicious. Might want to verify that the outputs are the same.

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

    Can you call numpy functions from within a cython kernel?

    • @rizzwan-42069
      @rizzwan-42069 4 місяці тому

      Based on vids that i watched numpy doesn't work with cython. So maybe look into numba.

  • @PeterRichardsandYoureNot
    @PeterRichardsandYoureNot 8 місяців тому

    So confused. In the second example. Why did you have to make a separate cpdef list function ? I don’t see how it is even referenced from the first routine? I thought the whole idea was just to Cython existing code. This is very odd as it looks like you have to add a bunch of anscillary code to make it work and how would you even know what to put? My brain hurts now.

  • @richardbennett4365
    @richardbennett4365 8 місяців тому +2

    😮😮😮
    Oh, no. The narrator said, "can be RAN."
    NO. CAN BE RUN. The word be is right there, so the past participle form must be used of the verb to run, not the past tense form of run.
    Run, ran, run. It's not run, ran, ran. That woukd be incorrect.

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

    how are classes with user functions represented in C?

    • @Apuryo
      @Apuryo 10 місяців тому

      no classes in c. use c++

  • @asianman1329
    @asianman1329 2 роки тому

    Could you do a video on mypyc?

    • @Carberra
      @Carberra  2 роки тому +1

      I certainly can! Planning on doing a video on that for next week (:

    • @asianman1329
      @asianman1329 2 роки тому

      @@Carberra sweet!

  • @2sourcerer
    @2sourcerer Рік тому

    Javascript also has to check types. Why is it faster?

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

    Factorial 1000 will overflow your int. C doesn't care about the overflow, python does.

  • @jfk1337
    @jfk1337 2 роки тому

    how can cython modules be pip installed?

    • @Carberra
      @Carberra  2 роки тому

      Same as any other module.

    • @jfk1337
      @jfk1337 2 роки тому

      @@Carberra And pip automatically builds the .so?

    • @Carberra
      @Carberra  2 роки тому

      Cython builds the .so files. You then include the .so files in your distributed package, and pip will treat it like any other file.

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

      @@Carberra and this makes your code no longer portable

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

    Could you please make a series of Cython? It's really amazing. 💯
    Thanksgiving.

    • @Carberra
      @Carberra  9 місяців тому

      If only I knew enough about it to say I was confident making a series on it 😅 I do keep meaning to go back to it and learn it more, just finding the time to do so atm is tough.

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

      @@Carberra I can understand your hard work. Please proceed with the action in the future if feasible. Thank you.

  • @furkanesen
    @furkanesen 2 роки тому

    Thx you.

  • @ritikranjan7192
    @ritikranjan7192 2 роки тому +3

    naise

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

    Just use Taichi.

  • @JOHNSMITH-ve3rq
    @JOHNSMITH-ve3rq Рік тому +2

    Bruh just paste the python code into gpt and ask it to turn it into pure c. Problem solved m

    • @hammerheadcorvette4
      @hammerheadcorvette4 9 місяців тому

      Learn to code. You can't rely on ChatGPT too much. Gemini is even worse.

  • @richardbennett4365
    @richardbennett4365 8 місяців тому

    Its wuick, because it's compiled into machine code. Duh.

  • @cachazurdos
    @cachazurdos 2 роки тому

    Spanish pliss