展开似乎确实有轻微的性能优势,但它可以忽略不计,所以除非你的do_something
函数真的几乎什么都不做,否则你不应该看到差异。我很难相信不同方法的等效行为可能达到 60%,尽管我总是愿意对一些我从未想过的实现细节感到惊讶。
tl;博士总结,使用 32500 而不是 325000 因为我不耐烦:
do_nothing easy 3.44702410698
do_nothing indexed 3.99766016006
do_nothing mapped 4.36127090454
do_nothing unrolled 3.33416581154
do_something easy 5.4152610302
do_something indexed 5.95649385452
do_something mapped 6.20316290855
do_something unrolled 5.2877831459
do_more easy 16.6573209763
do_more indexed 16.8381450176
do_more mapped 17.6184959412
do_more unrolled 16.0713188648
CPython 2.7.3,代码:
from timeit import Timer
nrows = 32500
ncols = 90
a = [[1.0*i for i in range(ncols)] for j in range(nrows)]
def do_nothing(x):
pass
def do_something(x):
z = x+3
return z
def do_more(x):
z = x**3+x**0.5+4
return z
def easy(rows, action):
for eachRow in rows:
for eachItem in eachRow:
action(eachItem)
def mapped(rows, action):
for eachRow in rows:
map(action, eachRow)
def indexed(rows, action):
for eachRow in rows:
for i in xrange(len(eachRow)):
action(eachRow[i])
def unrolled(rows, action):
for eachRow in rows:
action(eachRow[0])
action(eachRow[1])
action(eachRow[2])
action(eachRow[3])
action(eachRow[4])
action(eachRow[5])
action(eachRow[6])
action(eachRow[7])
action(eachRow[8])
action(eachRow[9])
action(eachRow[10])
action(eachRow[11])
action(eachRow[12])
action(eachRow[13])
action(eachRow[14])
action(eachRow[15])
action(eachRow[16])
action(eachRow[17])
action(eachRow[18])
action(eachRow[19])
action(eachRow[20])
action(eachRow[21])
action(eachRow[22])
action(eachRow[23])
action(eachRow[24])
action(eachRow[25])
action(eachRow[26])
action(eachRow[27])
action(eachRow[28])
action(eachRow[29])
action(eachRow[30])
action(eachRow[31])
action(eachRow[32])
action(eachRow[33])
action(eachRow[34])
action(eachRow[35])
action(eachRow[36])
action(eachRow[37])
action(eachRow[38])
action(eachRow[39])
action(eachRow[40])
action(eachRow[41])
action(eachRow[42])
action(eachRow[43])
action(eachRow[44])
action(eachRow[45])
action(eachRow[46])
action(eachRow[47])
action(eachRow[48])
action(eachRow[49])
action(eachRow[50])
action(eachRow[51])
action(eachRow[52])
action(eachRow[53])
action(eachRow[54])
action(eachRow[55])
action(eachRow[56])
action(eachRow[57])
action(eachRow[58])
action(eachRow[59])
action(eachRow[60])
action(eachRow[61])
action(eachRow[62])
action(eachRow[63])
action(eachRow[64])
action(eachRow[65])
action(eachRow[66])
action(eachRow[67])
action(eachRow[68])
action(eachRow[69])
action(eachRow[70])
action(eachRow[71])
action(eachRow[72])
action(eachRow[73])
action(eachRow[74])
action(eachRow[75])
action(eachRow[76])
action(eachRow[77])
action(eachRow[78])
action(eachRow[79])
action(eachRow[80])
action(eachRow[81])
action(eachRow[82])
action(eachRow[83])
action(eachRow[84])
action(eachRow[85])
action(eachRow[86])
action(eachRow[87])
action(eachRow[88])
action(eachRow[89])
def timestuff():
for action in 'do_nothing do_something do_more'.split():
for name in 'easy indexed mapped unrolled'.split():
t = Timer(setup="""
from __main__ import {} as fn
from __main__ import {} as action
from __main__ import a
""".format(name, action),
stmt="fn(a, action)").timeit(10)
print action, name, t
if __name__ == '__main__':
timestuff()
(请注意,我并没有费心使比较完全公平,因为我只是试图衡量变化的可能规模,即顺序统一的变化与否。)