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 repea
len
Using l = len(li) is faster:
l = len(li)
python -m timeit -s "li = [1, 2, 3]" "len(li)" 1000000 loops, best of 3: 0.239 usec per loop python -m timeit -s "li = [1, 2, 3]; l = len(li)" "l" 10000000 loops, best of 3: 0.0949 usec per loop