123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463 |
- #!/usr/bin/env python
- # encoding: utf-8
- import sys
- import time
- import gc
- from functools import wraps
- #------------------------------------------------------------------------------
- # [ timer function decorator ]
- # runs timed repetitions of the decorated function in a single trial
- # default is 100,000 repetitions in the trial
- # reports the results of the trial
- # Usage example:
- # from Naked.toolshed.benchmarking import timer
- # @timer
- # def myfunction():
- #------------------------------------------------------------------------------
- def timer(func, repetitions=100000):
- @wraps(func)
- def wrapper(*args, **kwargs):
- sys.stdout.write("Starting " + str(repetitions) + " repetitions of " + func.__name__ + "()...")
- sys.stdout.flush()
- print(" ")
- # disable garbage collection
- gc.collect()
- gc.disable()
- start = time.time()
- for x in range(repetitions):
- result = func(*args, **kwargs)
- end = time.time()
- gc.enable() # re-enable garbage collection
- gc.collect()
- print(str(repetitions) + " repetitions of " + func.__name__ + " : " + str(end-start) + " sec")
- return result
- return wrapper
- #------------------------------------------------------------------------------
- # [ timer_X function decorators ]
- # replicate the above decorator with different number of repetitions
- #------------------------------------------------------------------------------
- def timer_10(func, repetitions=10):
- @wraps(func)
- def wrapper(*args, **kwargs):
- sys.stdout.write("Starting " + str(repetitions) + " repetitions of " + func.__name__ + "()...")
- sys.stdout.flush()
- print(" ")
- # disable garbage collection
- gc.collect()
- gc.disable()
- start = time.time()
- for x in range(repetitions):
- result = func(*args, **kwargs)
- end = time.time()
- gc.enable() # re-enable garbage collection
- gc.collect()
- print(str(repetitions) + " repetitions of " + func.__name__ + " : " + str(end-start) + " sec")
- return result
- return wrapper
- def timer_100(func, repetitions=100):
- @wraps(func)
- def wrapper(*args, **kwargs):
- sys.stdout.write("Starting " + str(repetitions) + " repetitions of " + func.__name__ + "()...")
- sys.stdout.flush()
- print(" ")
- # disable garbage collection
- gc.collect()
- gc.disable()
- start = time.time()
- for x in range(repetitions):
- result = func(*args, **kwargs)
- end = time.time()
- gc.enable() # re-enable garbage collection
- gc.collect()
- print(str(repetitions) + " repetitions of " + func.__name__ + " : " + str(end-start) + " sec")
- return result
- return wrapper
- def timer_1k(func, repetitions=1000):
- @wraps(func)
- def wrapper(*args, **kwargs):
- sys.stdout.write("Starting " + str(repetitions) + " repetitions of " + func.__name__ + "()...")
- sys.stdout.flush()
- print(" ")
- # disable garbage collection
- gc.collect()
- gc.disable()
- start = time.time()
- for x in range(repetitions):
- result = func(*args, **kwargs)
- end = time.time()
- gc.enable() # re-enable garbage collection
- gc.collect()
- print(str(repetitions) + " repetitions of " + func.__name__ + " : " + str(end-start) + " sec")
- return result
- return wrapper
- def timer_10k(func, repetitions=10000):
- @wraps(func)
- def wrapper(*args, **kwargs):
- sys.stdout.write("Starting " + str(repetitions) + " repetitions of " + func.__name__ + "()...")
- sys.stdout.flush()
- print(" ")
- # disable garbage collection
- gc.collect()
- gc.disable()
- start = time.time()
- for x in range(repetitions):
- result = func(*args, **kwargs)
- end = time.time()
- gc.enable() # re-enable garbage collection
- gc.collect()
- print(str(repetitions) + " repetitions of " + func.__name__ + " : " + str(end-start) + " sec")
- return result
- return wrapper
- def timer_1m(func, repetitions=1000000):
- @wraps(func)
- def wrapper(*args, **kwargs):
- sys.stdout.write("Starting " + str(repetitions) + " repetitions of " + func.__name__ + "()...")
- sys.stdout.flush()
- print(" ")
- # disable garbage collection
- gc.collect()
- gc.disable()
- start = time.time()
- for x in range(repetitions):
- result = func(*args, **kwargs)
- end = time.time()
- gc.enable() # re-enable garbage collection
- gc.collect()
- print(str(repetitions) + " repetitions of " + func.__name__ + " : " + str(end-start) + " sec")
- return result
- return wrapper
- #------------------------------------------------------------------------------
- # [ timer_trials_benchmark decorator function ]
- # time a function and compare to a benchmark function
- # default is 10 trials x 100,000 repetitions/trial for each function
- # Results:
- # - Mean of the 10 trials of the test function
- # - standard deviation of the 10 trials (if numpy is available)
- # - result for 100,000 repetitions of the benchmark function
- # - ratio of the test : benchmark function results
- # Usage example:
- # from Naked.toolshed.benchmarking import timer_trials_benchmark
- # @timer_trials_benchmark
- # def myfunction():
- #------------------------------------------------------------------------------
- def timer_trials_benchmark(func, repetitions=100000, trials=10):
- @wraps(func)
- def wrapper(*args, **kwargs):
- sys.stdout.write("Starting timed trials of " + func.__name__ + "()")
- sys.stdout.flush()
- result_list = []
- benchmark_list = []
- # disable garbage collection
- gc.collect()
- gc.disable()
- for x in range(trials):
- # test function
- start = time.time()
- for y in range(repetitions):
- func(*args, **kwargs)
- end = time.time()
- result = func(*args, **kwargs)
- result_list.append(end-start)
- # benchmark function
- L = []
- start2 = time.time()
- for j in range(repetitions):
- for i in range(10):
- L.append(i)
- end2 = time.time()
- benchmark_list.append(end2 - start2)
- sys.stdout.write(".")
- sys.stdout.flush()
- gc.enable() # re-enable garbage collection
- gc.collect()
- print(" ")
- n = 1
- for run in result_list:
- print("Trial " + str(n) + ":\t" + str(run))
- n += 1
- print("-"*50)
- mean = sum(result_list)/len(result_list)
- mean_benchmark = sum(benchmark_list)/len(benchmark_list)
- print("Mean for " + str(repetitions) + " repetitions: " + str(mean) + " sec")
- try:
- import numpy as np
- array = np.array(result_list)
- print( "Standard Deviation: " + str(np.std(array)))
- except ImportError as ie:
- pass
- print("Mean per repetition: " + str(mean/repetitions) + " sec")
- print("Mean for " + str(repetitions) + " of benchmark function:" + str(mean_benchmark) + " sec")
- print("Ratio: " + str(mean/mean_benchmark))
- return result
- return wrapper
- #------------------------------------------------------------------------------
- # [ timer_trials_benchmark_X decorators ]
- # additional benchmark decorators that replicate the above function with different # repetitions
- #------------------------------------------------------------------------------
- def timer_trials_benchmark_10(func, repetitions=10, trials=10):
- @wraps(func)
- def wrapper(*args, **kwargs):
- sys.stdout.write("Starting timed trials of " + func.__name__ + "()")
- sys.stdout.flush()
- result_list = []
- benchmark_list = []
- # disable garbage collection
- gc.collect()
- gc.disable()
- for x in range(trials):
- # test function
- start = time.time()
- for y in range(repetitions):
- func(*args, **kwargs)
- end = time.time()
- result = func(*args, **kwargs)
- result_list.append(end-start)
- # benchmark function
- L = []
- start2 = time.time()
- for j in range(repetitions):
- for i in range(10):
- L.append(i)
- end2 = time.time()
- benchmark_list.append(end2 - start2)
- sys.stdout.write(".")
- sys.stdout.flush()
- gc.enable() # re-enable garbage collection
- gc.collect()
- print(" ")
- n = 1
- for run in result_list:
- print("Trial " + str(n) + ":\t" + str(run))
- n += 1
- print("-"*50)
- mean = sum(result_list)/len(result_list)
- mean_benchmark = sum(benchmark_list)/len(benchmark_list)
- print("Mean for " + str(repetitions) + " repetitions: " + str(mean) + " sec")
- try:
- import numpy as np
- array = np.array(result_list)
- print( "Standard Deviation: " + str(np.std(array)))
- except ImportError as ie:
- pass
- print("Mean per repetition: " + str(mean/repetitions) + " sec")
- print("Mean for " + str(repetitions) + " of benchmark function:" + str(mean_benchmark) + " sec")
- print("Ratio: " + str(mean/mean_benchmark))
- return result
- return wrapper
- def timer_trials_benchmark_100(func, repetitions=100, trials=10):
- @wraps(func)
- def wrapper(*args, **kwargs):
- sys.stdout.write("Starting timed trials of " + func.__name__ + "()")
- sys.stdout.flush()
- result_list = []
- benchmark_list = []
- # disable garbage collection
- gc.collect()
- gc.disable()
- for x in range(trials):
- # test function
- start = time.time()
- for y in range(repetitions):
- func(*args, **kwargs)
- end = time.time()
- result = func(*args, **kwargs)
- result_list.append(end-start)
- # benchmark function
- L = []
- start2 = time.time()
- for j in range(repetitions):
- for i in range(10):
- L.append(i)
- end2 = time.time()
- benchmark_list.append(end2 - start2)
- sys.stdout.write(".")
- sys.stdout.flush()
- gc.enable() # re-enable garbage collection
- gc.collect()
- print(" ")
- n = 1
- for run in result_list:
- print("Trial " + str(n) + ":\t" + str(run))
- n += 1
- print("-"*50)
- mean = sum(result_list)/len(result_list)
- mean_benchmark = sum(benchmark_list)/len(benchmark_list)
- print("Mean for " + str(repetitions) + " repetitions: " + str(mean) + " sec")
- try:
- import numpy as np
- array = np.array(result_list)
- print( "Standard Deviation: " + str(np.std(array)))
- except ImportError as ie:
- pass
- print("Mean per repetition: " + str(mean/repetitions) + " sec")
- print("Mean for " + str(repetitions) + " of benchmark function:" + str(mean_benchmark) + " sec")
- print("Ratio: " + str(mean/mean_benchmark))
- return result
- return wrapper
- def timer_trials_benchmark_1k(func, repetitions=1000, trials=10):
- @wraps(func)
- def wrapper(*args, **kwargs):
- sys.stdout.write("Starting timed trials of " + func.__name__ + "()")
- sys.stdout.flush()
- result_list = []
- benchmark_list = []
- # disable garbage collection
- gc.collect()
- gc.disable()
- for x in range(trials):
- # test function
- start = time.time()
- for y in range(repetitions):
- func(*args, **kwargs)
- end = time.time()
- result = func(*args, **kwargs)
- result_list.append(end-start)
- # benchmark function
- L = []
- start2 = time.time()
- for j in range(repetitions):
- for i in range(10):
- L.append(i)
- end2 = time.time()
- benchmark_list.append(end2 - start2)
- sys.stdout.write(".")
- sys.stdout.flush()
- gc.enable() # re-enable garbage collection
- gc.collect()
- print(" ")
- n = 1
- for run in result_list:
- print("Trial " + str(n) + ":\t" + str(run))
- n += 1
- print("-"*50)
- mean = sum(result_list)/len(result_list)
- mean_benchmark = sum(benchmark_list)/len(benchmark_list)
- print("Mean for " + str(repetitions) + " repetitions: " + str(mean) + " sec")
- try:
- import numpy as np
- array = np.array(result_list)
- print( "Standard Deviation: " + str(np.std(array)))
- except ImportError as ie:
- pass
- print("Mean per repetition: " + str(mean/repetitions) + " sec")
- print("Mean for " + str(repetitions) + " of benchmark function:" + str(mean_benchmark) + " sec")
- print("Ratio: " + str(mean/mean_benchmark))
- return result
- return wrapper
- def timer_trials_benchmark_10k(func, repetitions=10000, trials=10):
- @wraps(func)
- def wrapper(*args, **kwargs):
- sys.stdout.write("Starting timed trials of " + func.__name__ + "()")
- sys.stdout.flush()
- result_list = []
- benchmark_list = []
- # disable garbage collection
- gc.collect()
- gc.disable()
- for x in range(trials):
- # test function
- start = time.time()
- for y in range(repetitions):
- func(*args, **kwargs)
- end = time.time()
- result = func(*args, **kwargs)
- result_list.append(end-start)
- # benchmark function
- L = []
- start2 = time.time()
- for j in range(repetitions):
- for i in range(10):
- L.append(i)
- end2 = time.time()
- benchmark_list.append(end2 - start2)
- sys.stdout.write(".")
- sys.stdout.flush()
- gc.enable() # re-enable garbage collection
- gc.collect()
- print(" ")
- n = 1
- for run in result_list:
- print("Trial " + str(n) + ":\t" + str(run))
- n += 1
- print("-"*50)
- mean = sum(result_list)/len(result_list)
- mean_benchmark = sum(benchmark_list)/len(benchmark_list)
- print("Mean for " + str(repetitions) + " repetitions: " + str(mean) + " sec")
- try:
- import numpy as np
- array = np.array(result_list)
- print( "Standard Deviation: " + str(np.std(array)))
- except ImportError as ie:
- pass
- print("Mean per repetition: " + str(mean/repetitions) + " sec")
- print("Mean for " + str(repetitions) + " of benchmark function:" + str(mean_benchmark) + " sec")
- print("Ratio: " + str(mean/mean_benchmark))
- return result
- return wrapper
- def timer_trials_benchmark_1m(func, repetitions=1000000, trials=10):
- @wraps(func)
- def wrapper(*args, **kwargs):
- sys.stdout.write("Starting timed trials of " + func.__name__ + "()")
- sys.stdout.flush()
- result_list = []
- benchmark_list = []
- # disable garbage collection
- gc.collect()
- gc.disable()
- for x in range(trials):
- # test function
- start = time.time()
- for y in range(repetitions):
- func(*args, **kwargs)
- end = time.time()
- result = func(*args, **kwargs)
- result_list.append(end-start)
- # benchmark function
- L = []
- start2 = time.time()
- for j in range(repetitions):
- for i in range(10):
- L.append(i)
- end2 = time.time()
- benchmark_list.append(end2 - start2)
- sys.stdout.write(".")
- sys.stdout.flush()
- gc.enable() # re-enable garbage collection
- gc.collect()
- print(" ")
- n = 1
- for run in result_list:
- print("Trial " + str(n) + ":\t" + str(run))
- n += 1
- print("-"*50)
- mean = sum(result_list)/len(result_list)
- mean_benchmark = sum(benchmark_list)/len(benchmark_list)
- print("Mean for " + str(repetitions) + " repetitions: " + str(mean) + " sec")
- try:
- import numpy as np
- array = np.array(result_list)
- print( "Standard Deviation: " + str(np.std(array)))
- except ImportError as ie:
- pass
- print("Mean per repetition: " + str(mean/repetitions) + " sec")
- print("Mean for " + str(repetitions) + " of benchmark function:" + str(mean_benchmark) + " sec")
- print("Ratio: " + str(mean/mean_benchmark))
- return result
- return wrapper
- if __name__ == '__main__':
- pass
|