Speed Up Your Code With Cython

แชร์
ฝัง
  • เผยแพร่เมื่อ 1 ม.ค. 2025

ความคิดเห็น • 138

  • @Magmurrr
    @Magmurrr 3 ปีที่แล้ว +145

    When using the time module to benchmark something use time.perf_counter() rather than time.time() as it provides more precision and isn't the time since the epoch!

    • @jithin.johnson
      @jithin.johnson 2 ปีที่แล้ว +2

      thanks!

    • @calvindibartolo2686
      @calvindibartolo2686 ปีที่แล้ว +1

      I thought it was just a much more accurate time to the nanosecond or something?

    • @edwardb05
      @edwardb05 ปีที่แล้ว +4

      ​@@calvindibartolo2686 I'm late to this but for everyone else, perf_counter_ns() does nanoseconds and perf_counter() does seconds

    • @prawnydagrate
      @prawnydagrate ปีที่แล้ว

      @@edwardb05 similarly time.time_ns() does nanosecodns

  • @holo23
    @holo23 3 ปีที่แล้ว +44

    This is actually pretty useful
    I wasn't aware that cython was a thing until now
    Thanks for this!

  • @BobBeatski71
    @BobBeatski71 2 ปีที่แล้ว +7

    Here I was trying to attach Python to my C, when really I should try attaching C style to Python. 👍 Great vid. You've given me something to consider.

  • @barendscholtus1786
    @barendscholtus1786 3 ปีที่แล้ว +17

    If both functions are in the same source file, both will be compiled I assume? Have you tried putting the optimised function in a separate pyx file and only compiling that?
    Finally you might be able to do number += 2 because prime numbers cannot be even anyway.

  • @gaelc13
    @gaelc13 11 หลายเดือนก่อน +1

    It's not only about static typing, but also probably about predefined vs dynamic sized array. Dynamical array size definitely comes at a cost.

  • @hyakushiki23
    @hyakushiki23 2 ปีที่แล้ว +4

    Thanks again for a great video. I like that you use Windows as well.
    It was cool to see the speed of : flexibility of Python vs rigidness of cython

  • @rushas
    @rushas 3 ปีที่แล้ว +6

    Great video! I would love to watch more of those Cython vs Python

  • @isaacbaum6745
    @isaacbaum6745 3 ปีที่แล้ว +16

    This is an example of Cython working better, but how about a comprehensive tutorial on how to use it? Does it work when you're using machine learning libraries?

    • @DendrocnideMoroides
      @DendrocnideMoroides 2 ปีที่แล้ว +1

      well if you use it with something like NumPy which is already fast you will achive no speedup at all, if it is even possible

    • @NphiniT
      @NphiniT ปีที่แล้ว

      Actually Cython is used most commonly (in my experience) in data science to speed up processor intensive tasks

    • @lbgstzockt8493
      @lbgstzockt8493 ปีที่แล้ว

      Most/all machine learning libraries already use well-optimized Assembly/C++/Fortran Code under the hood, so you won`t get any speed improvements.

    • @NphiniT
      @NphiniT ปีที่แล้ว

      @@lbgstzockt8493 You will get improvements for the python code you actually use. Especially for expensive tasks.

  • @JackyVSO
    @JackyVSO 3 ปีที่แล้ว +6

    When I do "import main" in VSCode, it says "import main could not be resolved"

    • @dumsky7
      @dumsky7 ปีที่แล้ว

      is there an answer to this question

    • @09Drdray
      @09Drdray หลายเดือนก่อน

      You should def main if _name_ == '__main__'

  • @brucea9871
    @brucea9871 ปีที่แล้ว +11

    There is a way to make your prime detecting more efficient. The only even prime number is 2 so there is no need to test the remaining even numbers. It would be more efficient to insert 2 in the list at the beginning of the program, then start testing at 3 and increment the number to be tested by 2 each time instead of by 1 as you did. This would double the speed of the program since there would be only half as many numbers to be tested. Another thing to consider is that when testing a number for primality it is not necessary to check if any prime less than the number divides it. It is only necessary to check if any prime less than its square root divides it. But I'm not sure how much this would speed up the program (if at all) due to the overhead of calculating the square root of each number to be tested (unless you used a relatively simple formula to approximate the square root instead of calculating a precise value).

    • @comrade_marshal
      @comrade_marshal ปีที่แล้ว

      The sqrt n method will work faster always as the code will iterate sqrt n times. Calculating the sqrt n won't be affecting much as compared to running code for n/2 or n/4 times. For 1st few numbers, n/2 or n/4 method will dominate and that too even before the number 20. I used the sqrt n method in the super slow bash script and it accurately found that 10247693 is a prime within a second, while Python was even faster than that. Even if you scale it to find all primes over a range, the sqrt method is simplest AND fastest method to find primes

    • @jsseper
      @jsseper ปีที่แล้ว +1

      I think the point here was not how to make that particular algorithm faster, but to show the difference between regular python and cython

    • @brucea9871
      @brucea9871 ปีที่แล้ว +1

      @jesussevillaperez3639 I am well aware of that. I just wanted to point out an improvement to the algorithm. If my suggestion was incorporated it would still show the increase in speed provided by Cython since both the Python and Cython versions would be faster.

    • @Max-fw3qy
      @Max-fw3qy 5 หลายเดือนก่อน

      ​@brucea9871 you suggestion has no value here, he could do anything just to show the difference, it doesn't matter.....

    • @ocelotmadness6287
      @ocelotmadness6287 3 หลายเดือนก่อน

      As someone who's comp Sci fascination is mostly algorithms this is greatly appreciated

  • @totalpieceofhit6100
    @totalpieceofhit6100 2 ปีที่แล้ว +3

    Why [p for p in primes[:found]] instead of just primes[:found] you're literally iterating through each number of a list, putting it in another list and returning the new list. (?!?!?!?!)

  • @ajiteshkumar
    @ajiteshkumar 3 ปีที่แล้ว +8

    me: wants to know about cython because python is soo slow
    NeuralNine: I have read your mind, here you go.
    Thanks!

  • @alexandermylnikov7774
    @alexandermylnikov7774 3 ปีที่แล้ว +2

    shouldnt you initialise the primes array to size amount

    • @kaanx4464
      @kaanx4464 2 ปีที่แล้ว

      no, u cant initialize the size of an array in runtime, it needs to be clear what size it is in compile time. If u do it in runtime it could give you an stack overflow

  • @b391i
    @b391i 3 ปีที่แล้ว +1

    All respect for you bro 🙏

  • @victordvickie
    @victordvickie 3 ปีที่แล้ว +1

    Will there be a follow up with C-Extension and a comparison with cython?

  • @renesis888
    @renesis888 3 ปีที่แล้ว +1

    How does Cython compare vs PyPy in terms of speed ?

  • @prashanthb6521
    @prashanthb6521 2 ปีที่แล้ว +1

    Very well explained.

  • @brandonhornick6878
    @brandonhornick6878 ปีที่แล้ว

    How does he get the pyx to work. I get a message saying I can’t do it on the community version.

  • @EshaanIITM
    @EshaanIITM 6 หลายเดือนก่อน

    can someone please explain why we used "100000" when defining the "primes"? Why not use something like "primes[amount]"?

    • @vi8370
      @vi8370 5 หลายเดือนก่อน

      In C and C++ array size must be known at compile time. amount is known only at runtime(even if we use constant in parameter when we call the function).

  • @Luc1an_
    @Luc1an_ 3 ปีที่แล้ว +27

    I am faster than Cython😅

    • @Luc1an_
      @Luc1an_ 3 ปีที่แล้ว +2

      @@sofianbar.598 😄

    • @NeuralNine
      @NeuralNine  3 ปีที่แล้ว +4

      😂

    • @zombiekiller7101
      @zombiekiller7101 3 ปีที่แล้ว +2

      Cython app running on vaccum tubes

  • @cw9249
    @cw9249 2 ปีที่แล้ว

    i tried this but cython only created a .c file, no pyd. i cant import the compiled script

  • @vishnubalaji9500
    @vishnubalaji9500 ปีที่แล้ว

    so whats the advantage of using cython instead of ctypes?

  • @JonakT
    @JonakT 9 หลายเดือนก่อน

    Sir, can we make exe files of kivy program by simply naming it .pyx and compiling it?

  • @pakxo.
    @pakxo. 8 หลายเดือนก่อน

    ty, exactly what I wanted to know.

  • @mohsenhaddadi1117
    @mohsenhaddadi1117 3 ปีที่แล้ว +1

    The question is, do built-in python functions ;like count method for lists; use Cython?
    and if the answer is yes, is it much faster to use count method instead of using for loop in a list?

    • @WeebDweebTCG
      @WeebDweebTCG 3 ปีที่แล้ว

      My general understanding is that built in methods would be faster. Not sure about the Cython though

    • @savageautomations
      @savageautomations 3 ปีที่แล้ว

      A lot of built in stuff is optimised, not sure about count though

    • @jagaya3662
      @jagaya3662 3 ปีที่แล้ว +1

      I mean, you could just test it out ;)
      But generally: Yes, built-in functions and functionality is at least optimized but often written in C. So it will easily outperform anything you write in Python yourself.

  • @iniyan19
    @iniyan19 3 ปีที่แล้ว +1

    How is it compared to vanilla c ?

    • @Louisianish
      @Louisianish 3 ปีที่แล้ว

      Because Cython is compiled into C code, I’d imagine they’re equivalent in speed, but I’m also curious about the answer to this question.

  • @TheRealKitWalker
    @TheRealKitWalker 3 ปีที่แล้ว +1

    Thanks dude 👏👏✌️✌️👍

  • @dhanviner1983
    @dhanviner1983 ปีที่แล้ว

    NameError: name 'exit' is not defined
    my exit command gives me this error. How can I fix it?

  • @basic-1337
    @basic-1337 ปีที่แล้ว +2

    11:24 break my focus hahaah

  • @pavantippa2287
    @pavantippa2287 3 ปีที่แล้ว

    Thanks u saved my day

  • @WeebDweebTCG
    @WeebDweebTCG 3 ปีที่แล้ว

    Is there a Cuby?

  • @roccov3614
    @roccov3614 3 ปีที่แล้ว

    Woah! What's this for-else code? Can you you use else with for loops? I'll have to look that up. Didn't know about that. Learn something every day.

  • @magicpython3887
    @magicpython3887 2 ปีที่แล้ว

    it didn't work .when i did the command 'py .\setup.py build_ext --inplace' created a new file with title 'main.c' and written "#error Do not use this file, it is the result of a failed Cython compilation.
    " init. What i need to do?

    • @PolySorinYT
      @PolySorinYT หลายเดือนก่อน

      i got that err too idk

  • @anonymspro7864
    @anonymspro7864 3 ปีที่แล้ว

    Bro how can i run my python file on android like Suppose i've created a clock.py file and it can run easily in computer but not in android :( please help me.

  • @itaydvash
    @itaydvash 3 ปีที่แล้ว

    Why in prime_finder_optimized you didn't use the append method?

    • @thomasmeister8476
      @thomasmeister8476 3 ปีที่แล้ว +2

      because "primes" here it is a C-array, not a python list.

  • @tjbwhitehea1
    @tjbwhitehea1 3 ปีที่แล้ว +1

    Hey, what C compiler are you using for windows?

    • @LoamfyTools
      @LoamfyTools 5 หลายเดือนก่อน

      msvc (from Visual Studio Build Tools)

  • @cyancaelus6456
    @cyancaelus6456 2 ปีที่แล้ว

    First I Love C# and Python. Now I Love Cython too

  • @mad4hat
    @mad4hat 3 ปีที่แล้ว

    what the name of your outro song

  • @saurabhagrawal643
    @saurabhagrawal643 3 ปีที่แล้ว +2

    That's Great👍🏻

  • @stevehageman6785
    @stevehageman6785 11 หลายเดือนก่อน

    Thanks for the 'starter'. :-)

  • @zian.2493
    @zian.2493 3 ปีที่แล้ว

    how to make simple program for editing photos like upscaling its resolution

    • @WeebDweebTCG
      @WeebDweebTCG 3 ปีที่แล้ว

      Bro there’s probably quite a bit of fundamentals that go into that. Lol
      You might be able to find something online but that’s a project probably for more advanced programmers...not beginners.

  • @MagnusAnand
    @MagnusAnand 2 ปีที่แล้ว

    I love this video!!!

  • @Dbdlkxbdbwk
    @Dbdlkxbdbwk 3 ปีที่แล้ว +2

    Which code editor are you using ?

    • @dead3541
      @dead3541 3 ปีที่แล้ว

      Pycharm

    • @Dbdlkxbdbwk
      @Dbdlkxbdbwk 3 ปีที่แล้ว

      Looks different to my pycharm…

    • @abdulmuminyqn
      @abdulmuminyqn 3 ปีที่แล้ว

      @@Dbdlkxbdbwk pycharm with vim extension

  • @koooravevo8294
    @koooravevo8294 ปีที่แล้ว +1

    You made the optimized takes min of 1000 and amount
    So in case you pass amount higher than 1000 it will always take the 1000
    So all your work is like nothing and not practical at all

  • @martyrd0m
    @martyrd0m ปีที่แล้ว +1

    make a tutorial on compiling .pyx to .c to .exe. The executable should run without any dependencies

    • @ahmedelsayed3133
      @ahmedelsayed3133 ปีที่แล้ว

      Even with imported external pip modules?

  • @Alsexaren
    @Alsexaren ปีที่แล้ว

    When the language is so slow you have to use c code to make it bearable

  • @Saimon404
    @Saimon404 2 ปีที่แล้ว

    How to decompile cython to python ??

  • @saihitesh5816
    @saihitesh5816 2 ปีที่แล้ว

    why don't my pycharm accept .pyx ? it is just reading it as a text file

  • @adrienc8716
    @adrienc8716 ปีที่แล้ว

    Pyd is for Windows. How do we do for Linux ?

    • @LoamfyTools
      @LoamfyTools 5 หลายเดือนก่อน

      Compile it on Linux system or use WSL (Windows Subsystem for Linux)

  • @allenanderson4179
    @allenanderson4179 3 ปีที่แล้ว

    What IDE are you using?

  • @Nittrader
    @Nittrader 2 ปีที่แล้ว

    it's not working on big python script's

  • @s.aravindh6227
    @s.aravindh6227 3 ปีที่แล้ว +3

    Import turtle 🐢 small tutorial video bro 👍👍👍👍

  • @mikefocal5770
    @mikefocal5770 3 ปีที่แล้ว +7

    Hi NeuralNine, great Video, can ask this question : why using Python instead of C++?

    • @linuxrant
      @linuxrant ปีที่แล้ว

      because python is cool to write and readable and I hate squiggly brackets.

  • @arnavmehta3669
    @arnavmehta3669 3 ปีที่แล้ว

    Can opencv and cython combined?

    • @ckalas
      @ckalas 3 ปีที่แล้ว +2

      the thing is most computational libraries already call their own c/c++ code, things like numpy / opencv / tensorflow etc all are already optimized internally

  • @abhinavchoudhary8014
    @abhinavchoudhary8014 3 ปีที่แล้ว +7

    I learnt many things
    But I had a request to plz make a short playlist for these types of all modules which can useable for python
    Plz teaches us cython syntax and all stuff in detail as well as pandas and numpy
    I want to learn those thing
    Plz sir

  • @NamiberGames
    @NamiberGames 2 ปีที่แล้ว

    It's kinda dumb to ask but does cython have use cases ? Python isn't know to be optimised but then instead of using cython why not coding directly in C/C++ ?

    • @navjotsingh2251
      @navjotsingh2251 2 ปีที่แล้ว +6

      Cython definitely helps when doing high priority code. For example, machine learning models can benefit from cython if it's a massive model with massive data sets.
      Moreover, Django also uses cython in the background to speed up web development, as you want your server to be fast.
      Cython pretty much compiles down to well formatted C code, almost as if a professional C developer wrote it. It does this without you having to even know C at all.

  • @RAM-im5lr
    @RAM-im5lr 3 ปีที่แล้ว

    Please create a video on python user input in nested autocomplet

  • @cyancaelus6456
    @cyancaelus6456 2 ปีที่แล้ว

    I Love Cython too Now

  • @hectorminator4
    @hectorminator4 3 ปีที่แล้ว +6

    Yeah, you can also work with numpy arrays instead of regular lists and it will work much faster without all the need of that c complexity sintax, but great video anyway, I'll search about this way of programming

    • @navjotsingh2251
      @navjotsingh2251 2 ปีที่แล้ว +2

      Numpy is a C library, certainly knowing cython and C can help you extend numpy and heck even make it faster if you're running into problems you can program it at the C/Cython level to make optimizations.

  • @magic_2942
    @magic_2942 3 ปีที่แล้ว +1

    Personally I don't like Cython, If you want to speed up your python just use numba and with just one decorator you are set to go, and I would rather use Rust and bind it with python with pyo3 rather than using Cython

  • @dipeshsamrawat7957
    @dipeshsamrawat7957 9 หลายเดือนก่อน

    Thanksgiving 💯

  • @zuowang5185
    @zuowang5185 4 วันที่ผ่านมา

    Is cython still relevant in today?

  • @abdellasolomon6244
    @abdellasolomon6244 3 ปีที่แล้ว +3

    Very nice tutorial.
    please also make an Iron python tutorial.
    thanks.

  • @youtubegoogle4163
    @youtubegoogle4163 3 หลายเดือนก่อน

    You should've compared it with actual C version also

  • @GaryRichardson-x9x
    @GaryRichardson-x9x 3 หลายเดือนก่อน

    Stanton Shore

  • @dk4kja8
    @dk4kja8 3 หลายเดือนก่อน

    this is too hard for my tiny brain, im gonna stick to C

  • @philtoa334
    @philtoa334 2 ปีที่แล้ว

    Thx.

  • @mechaelbrun-kestler3868
    @mechaelbrun-kestler3868 3 ปีที่แล้ว +4

    The one complaint I had with Python was the type ambiguity.

    • @barendscholtus1786
      @barendscholtus1786 3 ปีที่แล้ว +1

      You can provide your type annotations in your python code, mypy can perform the linting, and your IDE the code analysis.

  • @MaryDylan-i9f
    @MaryDylan-i9f 2 หลายเดือนก่อน

    Jaquan Fields

  • @kritagyagupta9356
    @kritagyagupta9356 3 ปีที่แล้ว

    Subbed

  • @mimurajam1501
    @mimurajam1501 3 ปีที่แล้ว

    nice video

  • @samlolpip1604
    @samlolpip1604 3 ปีที่แล้ว

    Soooooooooo gret video pls make a lot of video about discord boy tnx

  • @BehruzZoirov-if5kw
    @BehruzZoirov-if5kw 7 หลายเดือนก่อน

    Python cython 😅😂😂😂😂

  • @troeteimarsch
    @troeteimarsch 3 ปีที่แล้ว

    nice videööööö :) grüße ;)

  • @LisaMiller-g8v
    @LisaMiller-g8v 3 หลายเดือนก่อน

    Garcia Betty Hernandez Jennifer Wilson Shirley

  • @TachBox71jr
    @TachBox71jr 10 หลายเดือนก่อน

    How To decoded Cython

  • @GCKteamKrispy
    @GCKteamKrispy ปีที่แล้ว

    So it's just Rust?😅

  • @atwork22
    @atwork22 ปีที่แล้ว

    Wow!!!

  • @hugopristauz3620
    @hugopristauz3620 ปีที่แล้ว

    cool

  • @zombiekiller7101
    @zombiekiller7101 3 ปีที่แล้ว

    Nice

  • @guilherme5094
    @guilherme5094 3 ปีที่แล้ว

    Like!

  • @calypso1441
    @calypso1441 3 ปีที่แล้ว

    mach mal videos auf deutsch auch

  • @crystal_royal3405
    @crystal_royal3405 3 ปีที่แล้ว

    Just use cpp lol

  • @navinvenkatesan9784
    @navinvenkatesan9784 3 ปีที่แล้ว

    Bro telegram bot

  • @ZenthmYT
    @ZenthmYT 2 ปีที่แล้ว

    Me #1k likes

  • @XyndraNerd
    @XyndraNerd 3 ปีที่แล้ว +1

    second

  • @monstertruck6612
    @monstertruck6612 3 ปีที่แล้ว

    Take regain with cold drink CO2 mix inject method in detal arm be young again

  • @stevenhe3462
    @stevenhe3462 3 ปีที่แล้ว

    Horrible program
    Great demonstration of Cython

  • @KogiSyl
    @KogiSyl 3 ปีที่แล้ว

    Omg, why don't you just program in language that is appropriate to the application.
    If you really need so fast module, make a library in c or even assembler if necessary.

    • @barendscholtus1786
      @barendscholtus1786 3 ปีที่แล้ว +4

      It IS a C module, just using Python syntax.

  • @undeadpresident
    @undeadpresident 2 ปีที่แล้ว

    I can't get this to work. Says there's a syntax error in _distutils_hack\__init__.py