Speeding Up Python Code With Caching

Поделиться
HTML-код
  • Опубликовано: 30 июн 2024
  • Today we learn how to speed up Python code using caching.
    ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
    📚 Programming Books & Merch 📚
    💻 The Algorithm Bible Book: www.neuralnine.com/books/
    🐍 The Python Bible Book: www.neuralnine.com/books/
    👕 Programming Merch: www.neuralnine.com/shop
    🌐 Social Media & Contact 🌐
    📱 Website: www.neuralnine.com/
    📷 Instagram: / neuralnine
    🐦 Twitter: / neuralnine
    🤵 LinkedIn: / neuralnine
    📁 GitHub: github.com/NeuralNine
    🎵 Outro Music From: www.bensound.com/
  • НаукаНаука

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

  • @ikki411
    @ikki411 3 года назад +22

    I just found out that the cache decorator was added in Python version 3.9, so those who are in a previous version have to use lru_cache instead.

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

    ahh... lemme cache my hello world program now

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

    You can increase the recursion limit using sys module
    import sys
    sys.setrecursionlimit(10**12)

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

      oh very good to know. Didn't know that ^^

    • @marcoh2537
      @marcoh2537 3 года назад +4

      Nice to know, but it looks like a dangerous tool to use!

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

      Be careful setting it too high. Too much recursion will cause a Segmentation Fault.

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

      OverflowError: Python int too large to convert to C int

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

    This is awesome! Learned much about it!

  • @Thokpower1
    @Thokpower1 3 года назад +4

    Actually learned so much from this. Decorators finally make sense and I found a way to speed up my python programs

  • @rugmaable
    @rugmaable 5 месяцев назад

    amazing video and content. Straight to the point.

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

    Me: "How do I do caching in python... I bet there's a module for this."
    NeuralNine: "yeah 'from functools import cache' "
    Perfect video, it's rare that you get the exact answer you are looking for with a walk through right away. I love the future!

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

    amazing video , just love it.

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

    Awesome as always 👍😀

  • @switchblade3868
    @switchblade3868 3 года назад +4

    When recommendation is faster than notification

  • @thomasgoodwin2648
    @thomasgoodwin2648 3 года назад +12

    When caching is used in calculations like this the technique is often called 'memoization'. Also quite handy when writing a prime factor sieve. Love the channel. Long live human 2.0. Go SpaceX.

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

    Thank u, and is there anyway u could make a video on Cython, Numba, or any other library helping speed ups?

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

    awesome toturial, thanks.
    but can we know how the cache function acts or not, and can we write a normal function acts like a built-in cache function.

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

    Thank you so much.

  • @user-lr1ws6eo5d
    @user-lr1ws6eo5d 2 года назад

    It was very helpful for me.

  • @mohamedhabas7391
    @mohamedhabas7391 3 года назад +6

    More of this content please !!
    actual real life use-full things
    thank you for the hard-work

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

      thanks for your kind words :)

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

    that's nice!!

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

    Amazing thanks

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

    Cool video!

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

    Loved your intro music!
    The video as well. You're awesome and unique as always :)

  • @Hello_-_-_-_
    @Hello_-_-_-_ 7 месяцев назад

    That is awesome

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

    I had already tried to import cache from functool in google colab but it got an error, do you know why this happens? So instead i import Cache from cache-decorator

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

    this cache is similar to the idea of dynamic - save some part of the result

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

    Where can you learn about this caching from first principles?

  • @mr.lawliet
    @mr.lawliet 3 года назад +3

    When I saw it I just hoped that it wasn’t an april fools

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

      That would not be the most catchy title for a joke xD

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

    Thanks

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

    Nice video

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

    Good better and best

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

    Search: Emanuel Swedenborg

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

    my python 3.9 functools dont have @cache anymore i think , i get an error whereas @lru_cache works

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

    Cool.....

  • @omaral-halabiah2851
    @omaral-halabiah2851 3 года назад

    helpful

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

    YES

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

    for me it isnt working. with cache and without cache always shows same time idk why

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

    great

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

    explanation is too clear !!!

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

    Why did time.time() function is useless for results under one seconds ? In fact, executing code is happening in microseconds and milliseconds.

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

    Caching data basically means you're decreasing the amount of calculations you need to do by optimizing the process through which the calculations are done.
    But heres what im confused about.
    Why does caching make it THAT much faster? Here you're decreasing the number of calculations u need to do by half (or is that more than half) so why is it so significantly faster
    Found my answer. The decrease in calculations is significantly more than half.
    The Fibonacci sequence is super unoptimized

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

    Do a video about cython

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

    Hello bro can you explain pwntools python library

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

    Ok, so if I have a pyinstaller .exe onefile, will cache speed that up too?

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

      Yes,
      Pyinstaller exe is basically a self extracting archive.

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

      I want to know this too

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

      Yeah all pyinstaller does is bundle the whole python runtime with the code, it won’t be any faster. So yes, this will increase execution speeds. One note, if you are using linux or cygwin you can install cython to compile python to C, that would result in super fast execution times.

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

      @@boxtalks7994 Oh great. Thanks!

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

    🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥

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

    And now should i Cache every function?

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

      I think you would only need to do it on iterative functions, if you wanna learn more about this sort of "caching" data to speedup programs, FreeCodeCamp have a dynamic programming tutorial which basically explains how to do caching and speedup this function with code and not just a decorator. It's nice to see how things work :)

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

    Pycharm? Vscode pls!

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

    when i am working with classes and "@property" can i use the "@cache" too???

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

    print([fib(n) for n in range(0, 10000)])
    something like that with the cache enables python to bypass the maximum depth recursion

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

    Does caching have a negative side?

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

      you store values in memory, only downside I can think of, the bigger the cache, the more memory it will consume.

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

      @@benoitgael2969 Thank you

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

    april first lol

  • @user-pp6mm5eo7d
    @user-pp6mm5eo7d 3 года назад

    But Pycharm is heavy

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

    Wieso wieder PyCharm?

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

    Might've been useful to show how to implement caching like that instead of just importing

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

    2nd

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

    1st

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

    Where have I seen this video before...? You copied that from someone right?

  • @mehrdad-mixtape7970
    @mehrdad-mixtape7970 2 года назад

    Hi bro, Thanks for tutorial. but I have problem for this:
    from functools import cache
    when I run my code give me error:
    ImportError: cannot import name 'cache' from 'functools' (/usr/lib/python3.8/functools.py)

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

      `cache` was added to python 3.9. You will have to use `@functools.lru_cache(maxsize=None)` for the same effect as `@functools.cache`
      So just import `lru_cache` from functools

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

    can you help me 🤦‍♀🚨🚨
    TypeError: unhashable type: 'numpy.ndarray'