code1:
list=[1,2,3,4,5]
for i in range(len(list)):
for j in range(len(list)):
print(i+j)
code2:
list=[1,2,3,4,5]
l=len(list)
for i in range(l):
for j in range(l):
print(i+j)
Dose code2 faster than code1?
code1:
list=[1,2,3,4,5]
for i in range(len(list)):
for j in range(len(list)):
print(i+j)
code2:
list=[1,2,3,4,5]
l=len(list)
for i in range(l):
for j in range(l):
print(i+j)
Dose code2 faster than code1?
There will be no noticeable difference. len(somelist) is a very fast O(1) operation. Lists have their length stored internally, so there's very little work to be done when you ask a list for its length.
To answer the question in the title:
Should I store
len(list)somewhere?
Obtaining the length of a list requires O(1) time and is usally quite fast. In your first code fragment you will obtain the length of the list O(n) times, but this is not significant compared to the O(n2) time complexity of the algorithm.
Strictly speaking we can of course not know the time complexity of calculating the length of a list (since any Python interpreter might do it differently). But the most popular interpreter CPython uses the following datastructure:
typedef struct {
PyObject_VAR_HEAD
PyObject **ob_item;
Py_ssize_t allocated;
} PyListObject;
It holds a field allocated that stores the number of cells that are occupied that can be retrieved in constant time.
Your program is however strange in the sense that you iterate over the indices of the list, and not over the elements of the list. In case you want to process the elements, it is both more elegant and efficient to use:
# processing the elements of the list:
some_list = [1,4,2,5]
for elem1 in some_list:
for elem2 in some_list:
print(elem1+elem2)
Finally I would like to add that Python was not really designed to be the most efficient programming environment there is. Python has chosen programmer's convenience over speed (since the rationale is that development time is more costly than processing time). So if obtaining the length of a list several times, is really a key issue, Python is probably not the good choice to process your list to begin with. In that case you better make use of libraries like numpy that are designed to do processing in batch. These libraries interface some array processing functions to Python, but do the processing itself in a more efficient environment.
There is no difference, because len() is a very cheap operation, you can easily check it:
$ cat code1.py
#!/usr/bin/env python3
for x in range(10000):
list=[1,2,3,4,5]
for i in range(len(list)):
for j in range(len(list)):
print(i+j)
$ cat code2.py
#!/usr/bin/env python3
for x in range(10000):
list=[1,2,3,4,5]
l=len(list)
for i in range(l):
for j in range(l):
print(i)
$ time ./code1.py >> /dev/null
real 0m0.153s
user 0m0.152s
sys 0m0.000s
$ time ./code2.py >> /dev/null
real 0m0.156s
user 0m0.152s
sys 0m0.000s
yes code2 is faster than code 1
`reason is in code 1 each time you are calculating len which means you are calculating length 0(len(list)*len(list)) where in code 2 you area calculating only one time length of list here is o(len(list).`
def abc():
list=[1,2,3,4,5]
for i in range(len(list)):
for j in range(len(list)):
print(i+j)
def mmm():
list = [1, 2, 3, 4, 5]
l = len(list)
for i in range(l):
for j in range(l):
print(i + j)
from time import time
t0 = time()
abc()
t1 = time()
mmm()
t2 = time()
print 'function1 takes %f' %(t1-t0)
print 'function2 takes %f' %(t2-t1)
output:
function1 takes 0.000091
function2 takes 0.000065