I have created a function that takes in a another function as parameter and calculates the run time of that particular function. but when i run it, i can not seem to understand why this is not working . Does any one know why ?
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)