A similar question has already been ask Cost of len() function here. However, this question looks at the cost of len
it self.
Suppose, I have a code that repeats many times len(List)
, every time is O(1)
, reading a variable is also O(1)
plus assigning it is also O(1)
.
As a side note, I find that n_files = len(Files)
is somewhat more readable than repeated len(Files)
in my code. So, that is already an incentive for me to do this.
You could also argue against me, that somewhere in the code Files
can be modified, so n_files
is no longer correct, but that is not the case.
My question is:
Is the a number of calls to len(Files)
after which accessing n_files
will be faster?
The len() function in Python has a very peculiar characteristic that one had often wondered about. It takes absolutely no time, and equal time, in calculating the lengths of iterable data structures(string, array, tuple, etc.), irrespective of the size or type of data. This obviously implies O(1) time complexity.
The function len() is one of Python's built-in functions. It returns the length of an object. For example, it can return the number of items in a list. You can use the function with many different data types.
The len() function returns the number of items in an object. When the object is a string, the len() function returns the number of characters in the string.
No. A list is an object. You assign a list to a name-reference with = .
A few results (time, in seconds, for one million calls), with a ten-element list using Python 2.7.10 on Windows 7; store
is whether we store the length or keeping calling len
, and alias
is whether or not we create a local alias for len
:
Store Alias n= 1 10 100
Yes Yes 0.862 1.379 6.669
Yes No 0.792 1.337 6.543
No Yes 0.914 1.924 11.616
No No 0.879 1.987 12.617
and a thousand-element list:
Store Alias n= 1 10 100
Yes Yes 0.877 1.369 6.661
Yes No 0.785 1.299 6.808
No Yes 0.926 1.886 11.720
No No 0.891 1.948 12.843
Conclusions:
len
repeatedly, even for n == 1
;len
can make a small improvement for larger n
where we aren't storing the result, but not as much as just storing the result would; andTest script:
def test(n, l, store, alias):
if alias:
len_ = len
len_l = len_(l)
else:
len_l = len(l)
for _ in range(n):
if store:
_ = len_l
elif alias:
_ = len_(l)
else:
_ = len(l)
if __name__ == '__main__':
from itertools import product
from timeit import timeit
setup = 'from __main__ import test, l'
for n, l, store, alias in product(
(1, 10, 100),
([None]*10,),
(True, False),
(True, False),
):
test_case = 'test({!r}, l, {!r}, {!r})'.format(n, store, alias)
print test_case, len(l),
print timeit(test_case, setup=setup)
Function calls in python are costly, so if you are 100% sure that the size of n_files
would not change when you are accessing its length from the variable, you can use the variable, if that is what is more readable for you as well.
An Example performance test for both accessing len(list)
and accessing from variable , gives the following result -
In [36]: l = list(range(100000))
In [37]: n_l = len(l)
In [40]: %timeit newn = len(l)
10000000 loops, best of 3: 92.8 ns per loop
In [41]: %timeit new_n = n_l
10000000 loops, best of 3: 33.1 ns per loop
Accessing the variable is always faster than using len()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With