Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Python automatically optimize/cache function calls?

Tags:

python

I'm relatively new to Python, and I keep seeing examples like:

def max_wordnum(texts):
    count = 0
    for text in texts:
        if len(text.split()) > count:
            count = len(text.split())
    return count

Is the repeated len(text.split()) somehow optimized away by the interpreter/compiler in Python, or will this just take twice the CPU cycles of storing len(text.split()) in a variable?

like image 839
Nic Cottrell Avatar asked Aug 11 '18 19:08

Nic Cottrell


People also ask

Does Python cache function calls?

One performance-enhancement technique common to many languages, and one Python can use too, is memoization—caching the results of a function call so that future calls with the same inputs don't have to be recomputed from scratch. Python provides a standard library utility, lru_cache , to do this.

How does Python implement cache?

One way to implement an LRU cache in Python is to use a combination of a doubly linked list and a hash map. The head element of the doubly linked list would point to the most recently used entry, and the tail would point to the least recently used entry.

Does Python optimize code?

Python code optimization is a way to make your program perform any task more efficiently and quickly with fewer lines of code, less memory, or other resources involved, while producing the right results. It's crucial when it comes to processing a large number of operations or data while performing a task.

What is a cache function in Python?

cache is a decorator that helps in reducing function execution for the same inputs using the memoization technique. The function returns the same value as lru_cache(maxsize=None) , where the cache grows indefinitely without evicting old values.


1 Answers

Duplicate expressions are not "somehow optimized away". Use a local variable to capture and re-use a result that is 'known not to change' and 'takes some not-insignificant time' to create; or where using a variable increases clarity.

In this case, it's impossible for Python to know that 'text.split()' is pure - a pure function is one with no side-effects and always returns the same value for the given input.

Trivially: Python, being a dynamically-typed language, doesn't even know the type of 'text' before it actually gets a value, so generalized optimization of this kind is not possible. (Some classes may provide their own internal 'cache optimizations', but digressing..)

As: even a language like C#, with static typing, won't/can't optimize away general method calls - as, again, there is no basic enforceable guarantee of purity in C#. (ie. What if the method returned a different value on the second call or wrote to the console?)

But: a Haskell, a Purely Functional language, has the option to not 'evaluate' the call twice, being a different language with different rules...

like image 94
user2864740 Avatar answered Oct 23 '22 11:10

user2864740