How “lru_cache” Can Make Your Functions Over 100X FASTER In Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 1 ส.ค. 2024
  • In this video we will be learning about how we can use lru_cache from functools to drastically increase the performance of our functions in Python. When used correctly this will lead to a MASSIVE performance boost.
    lru_cache documentation:
    docs.python.org/3/library/fun...
    ▶ Become job-ready with Python:
    www.indently.io
    ▶ Follow me on Instagram:
    / indentlyreels
    00:00 About lru_cache
    00:42 How to use lru_cache
    04:27 Cache information
    05:50 Clearing the cache
    06:24 More about lru_cache
    07:29 Another example with lru_cache
    09:58 Insane performance boost
    10:20 More cache information
    10:53 Documentation
    11:14 Bye bye

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

  • @AlexCernat
    @AlexCernat 10 หลายเดือนก่อน +37

    you should also specify that the cache works with pure functions; if a function has side effects and those effects are "needed", then the cache will break the functionality

  • @vorpal22
    @vorpal22 10 หลายเดือนก่อน +8

    I was just going to say that one of the best examples to show the usefulness of caching here is fibonacci numbers, but I'm glad that you went through that.

  • @cheesy_boya
    @cheesy_boya 10 หลายเดือนก่อน +5

    Thanks for the guides, man. They've helped me a lot in my projects. Hope you'll keep on making them!

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

    hands down! the fibonacci example literally blew my mind, great video

  • @darrenlefcoe
    @darrenlefcoe 10 หลายเดือนก่อน +7

    I make python coding videos, so feel that I am (partly) qualified to tell you that you produce excellent videos.
    You type hint python so well, that you should consider moving to a statically typed language !

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

      Thanks mate! I've jumped back and forth a lot with statically typed languages, and I still love the freedom you get with Python, even if type hints are not enforced. Maybe someday I will go back to something statically typed (I've heard mojo was something new and hot).

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

      @Indently you are welcome. Your expression of the language is the best I have seen (and I have watched thousands of hours from many good youtubers)...
      There is typescript, but I don't know if the community likes it sufficiently.
      Anyway, good work, so keep it up.

  • @sadhappy8860
    @sadhappy8860 10 หลายเดือนก่อน +2

    Interesting! I can think of some uses for this already. Especially for some math functions

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

    Damn!!! That's a huge improvement.

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

    Fibonacci example was amazing one to demonstrate this module, love it. ❤
    😎👌🏻
    😄🕶️👌🏻

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

    Super useful for video game development.

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

    First viewer and first like . Love the video . Well I've been doing python for 5 years and i know lru cache. But good one.

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

    Nice!

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

    great. Now I see how my code will be running faster. I can already see a use for this. OMG python now just got better.

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

    Key is same input should result same output. Do not use random, time, uuid, sequence etc …. Deterministic functions in oracle

  • @starmscloud
    @starmscloud 10 หลายเดือนก่อน +1

    Your explanation is always fantastic..
    one question - from where to get list of libraries available in python ?

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

      PyPi

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

    How you are imported measure and you can share with that

  • @KnightAmine
    @KnightAmine 10 หลายเดือนก่อน +2

    Thank you! But will this not be on the expense of the storage? If there is like an infinity of diffrent possibilities will that not cause any memory problems? 🤓 What are the actual limits? And when is Iru_cache best used or when to avoid it? (a SQL based function?) Can we save/load the cache so it is there if the script is newly started?

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

      It can grow without limit, which eventually will cause issues. You can clear it manually with cache_clear(). lru_cache doesn't in itself support save/load. LRU is best used, when you want to cache last used computations, and the previous computations is often used for (near) future computations (hence the name, least recently used cache). If your limit is very large, you can also use it for complex or long running computations, that have a high likelihood of repeating itself. This can also be useful, if you spend a lot of time doing external (e.g. API) calls that have a high likelihood of repeating. LRU is a "one size fits all", it's not always the fastest, but it is simple.
      The usecase isn't massive. But when you can use it, it can often be an extreme performance boost.

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

    My python version doesn't contain tools with measure function. Is it tools library or a your own one?

  • @Pawlo370
    @Pawlo370 10 หลายเดือนก่อน +1

    Yay

  • @brhoom.h
    @brhoom.h 6 หลายเดือนก่อน

    is it necessary to clear the cash in some point!? and why!?

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

    Nice speed up 😮and nice and simple.
    But something can’t ever get infinitely faster, though 😊There’s this pesky thing called physical boundaries in our universe 😅

    • @Indently
      @Indently  10 หลายเดือนก่อน +1

      My ignorance has no physical boundries 😎

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

      @@Indently 😁same here, I tried to patent my ignorance but someone smarter than beat me to it already 😂

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

    I'm really curious to know where the cache is "physically" stored? I'm guessing it should be stored somewhere by the runtime

    • @aflous
      @aflous 10 หลายเดือนก่อน +5

      it is just a simple python hash table stored in RAM

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

      Right, makes sense

  • @manolomonetha8238
    @manolomonetha8238 10 หลายเดือนก่อน +1

    Keep in mind that using caching might be opening pandora's box. There are several cache management issues that can occur...

  • @user-ld8lc4ex4m
    @user-ld8lc4ex4m 10 หลายเดือนก่อน

    is it dynamic programming?

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

      Caching is one technique that is used commonly in dynamic programming, but there are others... so it isn't equivalent to dynamic programming, but it's a very useful tool to implement dynamic programming when you can do so.

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

    The highest Fibonachi number which has ever been calculated seems to be the 150,000th number in the sequence, if chatGPT isn't lying.

  • @123FireSnake
    @123FireSnake 10 หลายเดือนก่อน +9

    Making recusrive fibonacci viable, ansolutely disgusting :D

    • @Indently
      @Indently  10 หลายเดือนก่อน +1

      Don't lie, I know you love it 😉

    • @123FireSnake
      @123FireSnake 10 หลายเดือนก่อน +1

      @@Indently caching like this, absolutely :D
      But i go out of my way to avoid recursion. "Every recursive function can be turned into a normal function with a loop" is all i ever needed to hear on recursion. That being said i don't know how bad recursion is in Python but my background is in Java and unless you want an out of memory exception because the call stack is too big you don't do recursion :D

    • @tycodj
      @tycodj 10 หลายเดือนก่อน +2

      ​@@123FireSnake tail call optimization can fix that!

    • @duerdum9
      @duerdum9 10 หลายเดือนก่อน +4

      @@123FireSnake Python has a default limit of 1000 recursions. It's to prevent accidental DoS. You can however increase this limit with a single function call.

    • @vytah
      @vytah 10 หลายเดือนก่อน +1

      ​@@tycodjFibonacci does not end with a tail call

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

    I am completely stuck with the "from tools import measure".
    Cannot import measure from tools.
    I have the tools package downloaded but there is no measure package there?

    • @Indently
      @Indently  10 หลายเดือนก่อน +1

      I said I created it, no one can import my implementation 😅

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

      @@Indently That is really terrible in my view. You should at least write this in the python comment code instead of rushing through the voice comment on this particular important point. I hope you can consider my viewpoint here. Yes, this may be strongly worded but I do like what you contribute in general (hence why I am subscribed).

    • @Indently
      @Indently  10 หลายเดือนก่อน +2

      It's not an important point, all it does is time the code as I mentioned.

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

      @@IndentlyI guess u used perf_counter function and used (end time- starttime) to measure actual time of performing a function

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

      ​​@@akhileshchaurasia7966There is Not much to guess, He literally Said that He used pref Count... You should Watch the Video before asking questions...

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

    Could you please share the code with us? It would definitely help! Thank you!

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

    will help me make money

  • @mr.technoid
    @mr.technoid 10 หลายเดือนก่อน

    Worst thing of all time is recursion because it makes code length shorter but problem is at the same time it makes program a lot slower.

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

      "Worst thing of all time is recursion..."
      It's actually incredibly handy, and if you're doing functional programming, which is increasing in popularity for many reasons, there are no loops: recursion IS your looping technique.
      Making your function tail-recursive (which you can almost always do via techniques like accumulators, continuation-passing, or state monads) mitigates most of this by reusing the stack frame.

    • @mr.technoid
      @mr.technoid 10 หลายเดือนก่อน

      @@vorpal22 Doesn't matter what ever you say truth is code become slower a lot.

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

      @@mr.technoidIn a compiled language, tail-call optimization often translates recursive code to the same or a very similar instruction set in assembly code that using a loop does.
      If what you're working on needs to be speedy, you probably won't be using Python anyway, unless you're just piecing together libraries that are implemented in C, like numpy and many of the ML libraries, and then you're not not going to be particularly concerned with functional programming. (Python support for FP isn't great.)
      That being said, functional programming is gaining popularity quite quickly. Even non-FP languages are integrating a lot of FP constructs (e.g. Java and C++). If you're working with pure FP, you would never use a loop. In languages like Haskell, there isn't even support for loops: you'd be using functions like map, filter, foldLeft, foldRight, etc. or if you needed to write an ADT, you'd implement it and its typeclass instances using recursion. If you're not familiar with FP, I'd highly recommend you familiarize yourself with it at least a bit: it's a really interesting and different way of doing things that deviates substantially from OOP and imperative paradigms, and has a lot of advantages. The concepts are harder to understand, but there's a lot of really cool elegance there building programs with referential transparency and pure functions, and capturing side effects in monads. Research shows that code written in FP is less error prone, and in a language like Haskell, if your code compiles, it's highly likely that it isn't going to contain runtime errors.
      On top of that, it's overdramatic to say "worst thing of all time is recursion..." because there are plenty of things "worse" than recursion.

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

    can we use two decorator for the same function like @lru_cash and @staticmethod

  • @8starsAND
    @8starsAND 4 หลายเดือนก่อน +1

    2025 -> Python „programmers” discovered caching XDDD

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

      My man commenting from the future in 2025