Which is faster, notation in the list, for and while? I was wondering, so I wrote various codes and measured them.
python2.7 windows7 Intel Core i5 CPU 2.4GHz Memory 4.0 GB
for single loop (1 million times) for double loop (1000 times x 1000 times) List comprehension (1 million times) List comprehension (1000 times x 1000 times) while single loop (1 million times) while double loop (1000 times x 1000 times)
When creating a list In case of + for (with or without range)
List comprehension was the fastest if you just made a list. When an if statement was included, it got a little worse overall, and the for and list comprehension notation did not change much. While was clearly slowed down in a double loop. I'm not sure why. When it comes to total value calculation, list comprehension is slower than for. This seems to mean that it takes time to sum one million pieces. The result of total value calculation + if is the same regardless of whether it is a list comprehension or for.
You can write the list comprehension notation for the time being, but if the process does not make use of the created list (such as simply taking sum), the cost for processing the list with other functions (sum () etc.) will increase. And can be slow. While should be careful about if of multiple loops.
seq1 = range(10**6)
seq2 = range(10**3)
num1 = 10**6
num2 = 10**3
v = 10**10
def for1():
ls = []
for i in seq1:
ls.append(v)
#print len(ls)
def for2():
ls = []
for i in seq2:
for j in seq2:
ls.append(v)
#print len(ls)
def for_range1():
ls = []
for i in range(10**6):
ls.append(v)
#print len(ls)
def for_range2():
ls = []
for i in range(10**3):
for j in range(10**3):
ls.append(v)
#print len(ls)
def for_listnai1():
[v for i in seq1]
def for_listnai2():
[v for i in seq2 for j in seq2]
def for_listnai_range1():
[v for i in range(10**6)]
def for_listnai_range2():
[v for i in range(10**3) for j in range(10**3)]
def forsum1():
s = 0
for i in seq1:
s += v
def forsum2():
s = 0
for i in seq2:
for j in seq2:
s += v
def for_listnaisum1():
sum([v for i in seq1])
def for_listnaisum2():
sum([v for i in seq2 for j in seq2])
def while1():
i = 0
ls = []
while i < num1:
ls.append(v)
i+=1
def while2():
i = 0
ls = []
while i < num2:
j = 0
while j < num2:
ls.append(v)
j+=1
i+=1
def whilesum1():
i = 0
s = 0
while i < num1:
s += v
i+=1
def whilesum2():
i = 0
s = 0
while i < num2:
j = 0
while j < num2:
s += v
j+=1
i+=1
def forif1():
ls = []
for i in seq1:
if v % 2:
ls.append(v)
def forif2():
ls = []
for i in seq2:
for j in seq2:
if v % 2:
ls.append(v)
def forlistnaiif1():
[v for i in seq1 if v % 2]
def forlistnaiif2():
[v for i in seq2 for j in seq2 if v % 2]
def whileif1():
ls = []
i=0
while i <num1:
ls.append(v)
i+=1
def whileif2():
ls = []
i=0
while i <num2:
j=0
while j<num2 :
if v % 2:
ls.append(v)
j+=1
i+=1
def forifsum1():
s = 0
for i in seq1:
if v % 2:
s+=v
def forifsum2():
s = 0
for i in seq2:
for j in seq2:
if v % 2:
s+=v
def forlistnaiifsum1():
sum([v for i in seq1 if v % 2])
def forlistnaiifsum2():
sum([v for i in seq2 for j in seq2 if v % 2])
def whileifsum1():
s = 0
i=0
while i <num1:
s+=v
i+=1
def whileifsum2():
s=0
i=0
while i <num2:
j=0
while j<num2 :
if v % 2:
s+=v
j+=1
i+=1
def exe(func,num=0):
import time
print "%s start:" % func
s = 0
for i in range(0,num):
start = time.time()
exec func
end = time.time()
s += end - start
print "%s finished: %d times." % (func,num)
print s
if __name__=='__main__':
exe("for1()",1)
exe("for2()",1)
exe("for_range1()",1)
exe("for_range2()",1)
exe("for_listnai1()",1)
exe("for_listnai2()",1)
exe("for_listnai_range1()",1)
exe("for_listnai_range2()",1)
exe("while1()",1)
exe("while2()",1)
exe("forsum1()",1)
exe("forsum2()",1)
exe("for_listnaisum1()",1)
exe("for_listnaisum2()",1)
exe("whilesum1()",1)
exe("whilesum2()",1)
exe("forif1()",1)
exe("forif2()",1)
exe("forlistnaiif1()",1)
exe("forlistnaiif2()",1)
exe("whileif1()",1)
exe("whileif2()",1)
exe("forifsum1()",1)
exe("forifsum2()",1)
exe("forlistnaiifsum1()",1)
exe("forlistnaiifsum2()",1)
exe("whileifsum1()",1)
exe("whileifsum2()",1)