gpt4 book ai didi

Python装饰器正确计时递归函数

转载 作者:行者123 更新时间:2023-11-28 17:56:00 25 4
gpt4 key购买 nike

我正在编写一段用于研究目的的代码,我想比较使用不同算法对列表进行排序所需的时间。我尝试使用装饰器,但由于 mergeSort 函数是递归的,它为我提供了每次递归的结果。如果可能的话,我想找到一种方法来总结结果。由于我对装饰器很陌生,所以我不确定在那种情况下可以做什么。有没有办法使用装饰器实现该目标?

import random
import functools
import time


def timeIt(func):
@functools.wraps(func)
def newfunc(*args, **kwargs):
startTime = time.time()
func(*args, **kwargs)
elapsedTime = time.time() - startTime
print('function [{}] finished in {} ms'.format(
func.__name__, int(elapsedTime * 1000)))
return newfunc


@timeIt
def mergeSort(L):
if len(L) > 1:
mid = len(L) // 2
left = L[:mid]
right = L[mid:]
mergeSort(left)
mergeSort(right)
i = j = k = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
L[k] = left[i]
i += 1
else:
L[k] = right[j]
j += 1
k += 1
while i < len(left):
L[k] = left[i]
i += 1
k += 1
while j < len(right):
L[k] = right[j]
j += 1
k += 1


@timeIt
def selectionSort(L):
for fillslot in range(len(L) - 1, 0, -1):
maxpos = 0
for location in range(1, fillslot + 1):
if L[location] > L[maxpos]:
maxpos = location
temp = L[fillslot]
L[fillslot] = L[maxpos]
L[maxpos] = temp


randomList = random.sample(range(10000), 10000)
mergeSort(randomList.copy())
selectionSort(randomList.copy())

输出:

[...] truncated
function [mergeSort] finished in 7 ms
function [mergeSort] finished in 15 ms
function [mergeSort] finished in 33 ms
function [mergeSort] finished in 68 ms
function [selectionSort] finished in 2049 ms

最佳答案

您可以在包装函数上设置一个属性(示例中的 _entered)作为标志,以便它可以在设置属性时告诉它在递归调用中:

def timeIt(func):
@functools.wraps(func)
def newfunc(*args, **kwargs):
if not hasattr(newfunc, '_entered'): # enter only if _entered is not set
newfunc._entered = True # set _entered
startTime = time.time()
func(*args, **kwargs)
elapsedTime = time.time() - startTime
print('function [{}] finished in {} ms'.format(
func.__name__, int(elapsedTime * 1000)))
del newfunc._entered # remove _entered
return newfunc

关于Python装饰器正确计时递归函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58382733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com