-1
私は、別の関数をパラメータとしてとり、その特定の関数の実行時間を計算する関数を作成しました。しかし、私はそれを実行すると、なぜこれが動作していないのか分からないようです。だれかが理由を知っていますか?与えられた関数の実行時間を計算する
import time
import random
import timeit
import functools
def ListGenerator(rangeStart,rangeEnd,lenth):
sampleList = random.sample(range(rangeStart,rangeEnd),lenth)
return sampleList
def timeit(func):
@functools.wraps(func)
def newfunc(*args):
startTime = time.time()
func(*args)
elapsedTime = time.time() - startTime
print('function [{}] finished in {} ms'.format(
func.__name__, int(elapsedTime * 1000)))
return newfunc
@timeit
def bubbleSort(NumList):
compCount,copyCount= 0,0
for currentRange in range(len(NumList)-1,0,-1):
for i in range(currentRange):
compCount += 1
if NumList[i] > NumList[i+1]:
temp = NumList[i]
NumList[i] = NumList[i+1]
NumList[i+1] = temp
# print("Number of comparisons:",compCount)
NumList = ListGenerator(1,200,10)
print("Before running through soriting algorithm\n",NumList)
print("\nAfter running through soriting algorithm")
bubbleSort(NumList)
print(NumList,"\n")
for i in range (0, 10, ++1):
print("\n>Test run:",i+1)
bubbleSort(NumList)
compCount = ((len(NumList))*((len(NumList))-1))/2
print("Number of comparisons:",compCount)
実行時の画面は、コードだけで信じられないほどの高速実行のように見えます
あなたは 'timeit'をモジュールとデコレータの名前として持っています。それは大丈夫ですか? –
はい私はデコレータの名前を変更して、もう一度実行しました。バグがまだ残っています:/ @vishes_shell –
'bubbleSort(list(range(10000)))'を実行するだけです –