Python timeit.timeit - 片段版本的排序比使用 lambda 运行得更快,为什么?
Python timeit.timeit - snippet version of sort runs faster than using lambda, why?
我正在尝试使用 timeit.timeit 测量两个版本代码的排序时间 - 使用 lambda 的代码片段或正常代码:
bubble='''
elements = [1,22,4,632,12,3432,54531,2,2,2,2,2,4,54,3,2,1,54,43,11,22,32]
def bubble_sort(elements):
n=0
for i in range(len(elements)-n):
for j in range(len(elements)-1):
if elements[j]>elements[j+1]:
elements[j+1],elements[j] = elements[j], elements[j+1]
n+=1
return elements
'''
正常:
elements = [1,22,4,632,12,3432,54531,2,2,2,2,2,4,54,3,2,1,54,43,11,22,32]
def bubble_sort(elements):
n=0
for i in range(len(elements)-n):
for j in range(len(elements)-1):
if elements[j]>elements[j+1]:
elements[j+1],elements[j] = elements[j], elements[j+1]
n+=1
return elements
当我 运行 timeit.timeit(stmt=bubble, number=1000)
它给我结果 0.00026829999978872365
但是当我使用
t = Timer(lambda: bubble_sort(elements))
print(t.timeit(number = 1000))
它给出 0.04926030000024184,这要慢得多。有人可以解释一下为什么吗?
为了证实@Craig 的敏锐观察,实际调用片段版本中的函数证实它们几乎一样快。
import timeit
bubble='''
def bubble_sort(elements):
n=0
for i in range(len(elements)-n):
for j in range(len(elements)-1):
if elements[j]>elements[j+1]:
elements[j+1],elements[j] = elements[j], elements[j+1]
n+=1
return elements
bubble_sort([1,22,4,632,12,3432,54531,2,2,2,2,2,4,54,3,2,1,54,43,11,22,32])
'''
def bubble_sort(elements):
n=0
for i in range(len(elements)-n):
for j in range(len(elements)-1):
if elements[j]>elements[j+1]:
elements[j+1],elements[j] = elements[j], elements[j+1]
n+=1
return elements
print(timeit.timeit(stmt=bubble, number=10000))
print(timeit.timeit(stmt=lambda: bubble_sort([1,22,4,632,12,3432,54531,2,2,2,2,2,4,54,3,2,1,54,43,11,22,32]), number=10000))
print(timeit.timeit(stmt=lambda: list(sorted([1,22,4,632,12,3432,54531,2,2,2,2,2,4,54,3,2,1,54,43,11,22,32])), number=10000))
当然,除了练习之外,您不应该自己进行冒泡排序。
0.569853096
0.5863851120000001
0.00920665399999998
我正在尝试使用 timeit.timeit 测量两个版本代码的排序时间 - 使用 lambda 的代码片段或正常代码:
bubble='''
elements = [1,22,4,632,12,3432,54531,2,2,2,2,2,4,54,3,2,1,54,43,11,22,32]
def bubble_sort(elements):
n=0
for i in range(len(elements)-n):
for j in range(len(elements)-1):
if elements[j]>elements[j+1]:
elements[j+1],elements[j] = elements[j], elements[j+1]
n+=1
return elements
'''
正常:
elements = [1,22,4,632,12,3432,54531,2,2,2,2,2,4,54,3,2,1,54,43,11,22,32]
def bubble_sort(elements):
n=0
for i in range(len(elements)-n):
for j in range(len(elements)-1):
if elements[j]>elements[j+1]:
elements[j+1],elements[j] = elements[j], elements[j+1]
n+=1
return elements
当我 运行 timeit.timeit(stmt=bubble, number=1000)
它给我结果 0.00026829999978872365
但是当我使用
t = Timer(lambda: bubble_sort(elements))
print(t.timeit(number = 1000))
它给出 0.04926030000024184,这要慢得多。有人可以解释一下为什么吗?
为了证实@Craig 的敏锐观察,实际调用片段版本中的函数证实它们几乎一样快。
import timeit
bubble='''
def bubble_sort(elements):
n=0
for i in range(len(elements)-n):
for j in range(len(elements)-1):
if elements[j]>elements[j+1]:
elements[j+1],elements[j] = elements[j], elements[j+1]
n+=1
return elements
bubble_sort([1,22,4,632,12,3432,54531,2,2,2,2,2,4,54,3,2,1,54,43,11,22,32])
'''
def bubble_sort(elements):
n=0
for i in range(len(elements)-n):
for j in range(len(elements)-1):
if elements[j]>elements[j+1]:
elements[j+1],elements[j] = elements[j], elements[j+1]
n+=1
return elements
print(timeit.timeit(stmt=bubble, number=10000))
print(timeit.timeit(stmt=lambda: bubble_sort([1,22,4,632,12,3432,54531,2,2,2,2,2,4,54,3,2,1,54,43,11,22,32]), number=10000))
print(timeit.timeit(stmt=lambda: list(sorted([1,22,4,632,12,3432,54531,2,2,2,2,2,4,54,3,2,1,54,43,11,22,32])), number=10000))
当然,除了练习之外,您不应该自己进行冒泡排序。
0.569853096
0.5863851120000001
0.00920665399999998