Speed Up Your Code With Cython

Поделиться
HTML-код
  • Опубликовано: 16 янв 2025

Комментарии • 139

  • @Magmurrr
    @Magmurrr 3 года назад +147

    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

    • @prawnydagrate
      @prawnydagrate 11 дней назад

      @@calvindibartolo2686 no it's not just that. time.time() gives the time of the system clock, in seconds since the unix epoch. this means if you set your system clock back while work is being done, you could get a negative duration. time.perf_counter only counts forward, and its not the system time

  • @holo23
    @holo23 3 года назад +46

    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.

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

  • @barendscholtus1786
    @barendscholtus1786 3 года назад +18

    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.

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

  • @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!

  • @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 6 месяцев назад

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

    • @ocelotmadness6287
      @ocelotmadness6287 4 месяца назад

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

  • @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 2 месяца назад

      You should def main if _name_ == '__main__'

  • @basic-1337
    @basic-1337 Год назад +2

    11:24 break my focus hahaah

  • @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. (?!?!?!?!)

  • @b391i
    @b391i 3 года назад +1

    All respect for you bro 🙏

  • @Luc1an_
    @Luc1an_ 3 года назад +27

    I am faster than Cython😅

  • @prashanthb6521
    @prashanthb6521 2 года назад +1

    Very well explained.

  • @pakxo.
    @pakxo. 8 месяцев назад

    ty, exactly what I wanted to know.

  • @Alsexaren
    @Alsexaren Год назад

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

  • @TheRealKitWalker
    @TheRealKitWalker 3 года назад +1

    Thanks dude 👏👏✌️✌️👍

  • @victordvickie
    @victordvickie 3 года назад +1

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

  • @cyancaelus6456
    @cyancaelus6456 2 года назад

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

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

  • @pavantippa2287
    @pavantippa2287 3 года назад

    Thanks u saved my day

  • @WeebDweebTCG
    @WeebDweebTCG 3 года назад

    Is there a Cuby?

  • @brandonhornick6878
    @brandonhornick6878 Год назад

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

  • @stevehageman6785
    @stevehageman6785 11 месяцев назад

    Thanks for the 'starter'. :-)

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

  • @renesis888
    @renesis888 3 года назад +1

    How does Cython compare vs PyPy in terms of speed ?

  • @cyancaelus6456
    @cyancaelus6456 2 года назад

    I Love Cython too Now

  • @saurabhagrawal643
    @saurabhagrawal643 3 года назад +2

    That's Great👍🏻

  • @mad4hat
    @mad4hat 3 года назад

    what the name of your outro song

  • @vishnubalaji9500
    @vishnubalaji9500 Год назад

    so whats the advantage of using cython instead of ctypes?

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

  • @s.aravindh6227
    @s.aravindh6227 3 года назад +3

    Import turtle 🐢 small tutorial video bro 👍👍👍👍

  • @cw9249
    @cw9249 2 года назад

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

  • @tjbwhitehea1
    @tjbwhitehea1 3 года назад +1

    Hey, what C compiler are you using for windows?

    • @LoamfyTools
      @LoamfyTools 6 месяцев назад

      msvc (from Visual Studio Build Tools)

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

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

  • @MagnusAnand
    @MagnusAnand 2 года назад

    I love this video!!!

  • @zuowang5185
    @zuowang5185 18 дней назад

    Is cython still relevant in today?

  • @EshaanIITM
    @EshaanIITM 6 месяцев назад

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

    • @vi8370
      @vi8370 6 месяцев назад

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

  • @dhanviner1983
    @dhanviner1983 Год назад

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

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

  • @JonakT
    @JonakT 10 месяцев назад

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

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

  • @dipeshsamrawat7957
    @dipeshsamrawat7957 9 месяцев назад

    Thanksgiving 💯

  • @RAM-im5lr
    @RAM-im5lr 3 года назад

    Please create a video on python user input in nested autocomplet

  • @martyrd0m
    @martyrd0m Год назад +1

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

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

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

  • @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?

  • @philtoa334
    @philtoa334 2 года назад

    Thx.

  • @GaryRichardson-x9x
    @GaryRichardson-x9x 4 месяца назад

    Stanton Shore

  • @dk4kja8
    @dk4kja8 3 месяца назад

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

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

  • @adrienc8716
    @adrienc8716 Год назад

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

    • @LoamfyTools
      @LoamfyTools 6 месяцев назад

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

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

  • @Saimon404
    @Saimon404 2 года назад

    How to decompile cython to python ??

  • @allenanderson4179
    @allenanderson4179 3 года назад

    What IDE are you using?

  • @Nittrader
    @Nittrader 2 года назад

    it's not working on big python script's

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

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

  • @youtubegoogle4163
    @youtubegoogle4163 3 месяца назад

    You should've compared it with actual C version also

  • @abdellasolomon6244
    @abdellasolomon6244 3 года назад +3

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

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

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

  • @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 3 месяца назад

    Jaquan Fields

  • @kritagyagupta9356
    @kritagyagupta9356 3 года назад

    Subbed

  • @hugopristauz3620
    @hugopristauz3620 Год назад

    cool

  • @GCKteamKrispy
    @GCKteamKrispy Год назад

    So it's just Rust?😅

  • @mimurajam1501
    @mimurajam1501 3 года назад

    nice video

  • @samlolpip1604
    @samlolpip1604 3 года назад

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

  • @atwork22
    @atwork22 Год назад

    Wow!!!

  • @zombiekiller7101
    @zombiekiller7101 3 года назад

    Nice

  • @BehruzZoirov-if5kw
    @BehruzZoirov-if5kw 7 месяцев назад

    Python cython 😅😂😂😂😂

  • @LisaMiller-g8v
    @LisaMiller-g8v 4 месяца назад

    Garcia Betty Hernandez Jennifer Wilson Shirley

  • @saihitesh5816
    @saihitesh5816 2 года назад

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

  • @TachBox71jr
    @TachBox71jr 11 месяцев назад

    How To decoded Cython

  • @troeteimarsch
    @troeteimarsch 3 года назад

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

  • @calypso1441
    @calypso1441 3 года назад

    mach mal videos auf deutsch auch

  • @crystal_royal3405
    @crystal_royal3405 3 года назад

    Just use cpp lol

  • @guilherme5094
    @guilherme5094 3 года назад

    Like!

  • @navinvenkatesan9784
    @navinvenkatesan9784 3 года назад

    Bro telegram bot

  • @XyndraNerd
    @XyndraNerd 3 года назад +1

    second

  • @ZenthmYT
    @ZenthmYT 2 года назад

    Me #1k likes

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

  • @undeadpresident
    @undeadpresident 2 года назад

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