Skip to content

Instantly share code, notes, and snippets.

@wf9a5m75
Last active June 25, 2022 02:50
Show Gist options
  • Save wf9a5m75/e58a761b1ce02252e543d9466ce7bd1b to your computer and use it in GitHub Desktop.
Save wf9a5m75/e58a761b1ce02252e543d9466ce7bd1b to your computer and use it in GitHub Desktop.
import time
from typing import List
from random import randint
def measure(func):
def wrapper(*args, **kargs):
start_time = time.perf_counter()
result = func(*args, **kargs)
end_time = time.perf_counter()
execution_time = end_time - start_time
return [int(execution_time * 1000 * 1000), result]
# print(f'{func.__name__}: {execution_time}')
# return result
return wrapper
def test(caseName, func, nums, expectResult):
result = measure(func)(nums)
testResult = "pass" if result[1] == expectResult else "fail"
return [result[0], caseName, testResult]
def runTests(func):
results = []
results.append(test("test1", func, [-2,1,-3,4,-1,2,1,-5,4], 6))
results.append(test("test2", func, [1], 1))
results.append(test("test3", func, [5,4,-1,7,8], 23))
results.append(test("test4", func, [11, 74, 34, 94, 42, 100, -64, -72, -63, -3, 49, 71, 75, 30, 24, 8, -34, 73, -3, -25, 14, -16, -5, -87, 85, -37, 30, 91, 34, 40], 570))
results.append(test("test5", func, [609, 945, -227, 3, 889, 565, 181, 47, 345, 710, -410, -906, 980, -603, 490, 650, 52, -210, -868, 811, -751, 81, 690, 962, -422, -398, -797, 479, 949, -642, 972, -689, 101, -384, 993, -586, 407, 470, -892, -868, -665, -694, -563, 156, -885, -203, 557, -750, 135, 768, 530, 774, -191, 273, 813, -365, -797, -100, 730, 821, -887, -213, -2, -695, 126, 696, -55, 819, -147, -607, -482, -212, 872, 880, 345, -806, -99, 698, -337, -256, -87, -709, 301, 956, -629, -660, -926, -611, -975, -621, 449, -348, -440, -194, 907, 888, 687, 153, -74, -117], 5488))
total = 0
for result in results:
total += result[0]
print(result)
print("total = {} msec".format(total / 1000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment