Is it better for performance to use min() mutliple times or to store it in a variable?

Viewed 117

I have a small python code that uses min(list) multiple times on the same unchanged list, this got me wondering if I use the result of functions like min(), len(), etc... multiple times on an unchanged list is it better for me to store those results in variables, does it affect memory usage / performance at all?

If I had to guess I'd say that if a function like min() gets called many times it'd be better for performance to store it in a variable, but I am not sure of this since I don't really know how python gets the value or if python automatically stores this value somewhere as long as the list isn't changed.

5 Answers

Speed

It is almost always cheaper to store the result and re-use it rather than re-call the function multiple times.

Python does not cache (store and later remember) results from functions like min(), len(), etc.

Here is a quick speed test:

timeit.timeit("c = min(x) + min(x)", "x = [1, 2, 3]")
0.24990593400000005

timeit.timeit("a = min(x); b = a + a", "x = [1, 2, 3]")
0.1296667110000005

The second is almost twice as fast, because storing a variable is much cheaper than re-calling the min function.

Memory use

If the result is a single number, as with min() or len(), then memory use is negligible.

If the result is something substantial (e.g. a large table of values), then you can remove it when you're done with it using del

large_object = expensive_function()
do_something(large_object)
do_something_else(large_object)
del large_object

Also, large objects will automatically be deleted from memory when they fall out of scope (e.g. when a function returns) or when garbage collection rounds happen at regular intervals. For this reason, del is only necessary in certain circumstances like when dealing with circular references to an object.

min() is very fast compared to many other operations, such as I/O. So the efficiency improvements could be small for short lists and only a few repeated calls. However, if you cache the results of min(), you can realize some time savings. See the code below for examples of time you can actually save. As you can see, you need multiple iterations of the loops that contain min() calls to get any substantial the time savings.

import timeit

lst = range(2)

def test_min():
    x = [min(lst) for i in range(10)]

def test_cached_min():
    min_lst = min(lst)
    x = [min_lst for i in range(10)]

print(timeit.timeit("test_min()", globals = locals(), number = 1000))
print(timeit.timeit("test_cached_min()", globals = locals(), number = 1000))

# lst = range(2):
# 0.0027015960000000006
# 0.0010772920000000005

# lst = range(2000):
# 0.5262554810000001
# 0.05257684900000004

functions like min or max definitely have to traverse the array each time (giving them a complexity of O(n)). So yeah, specially if your array is larger, it's a better idea to store it in a variable rather than performing the calculation again.

More details about performance in another question

Time Complexity of Python List Operations

Complexity of List Operations

Source

The table shows that:

  • function len (to get length) has complexity O(1) (so very fast, so already stored)
  • function min (to get minimum) is O(n) (depends upon size of list, so computed each time).

This means that:

  • len does not need to be stored for reuse
  • min should be stored for reuse (especially for large lists)

If you are only using it 1-5 times, it doesn't really matter. But if you are going to call it anymore, and really less too, it is best to just save it as a variable. It will take next to no memory, and very little time to do so and to pull it from memory.

Related