def manual_time(filename, compare = False): """Calculates the runtime of given functions using time module and writes the results to file Args: filename (str): Filename of the report Compares (bool): A variable to extract the runtime using timeit module """ total_time = [] function_names = ["random_array", "snake_loop", "loop"] # Timing the functions iterations = 3 for i in range(iterations): start_time = time.time() array = tst.random_array(1e5) end_time = time.time() a = round(end_time - start_time, 5) start_time = time.time() filtered_array = tst.snake_loop(array) end_time = time.time() b = round(end_time - start_time, 5) start_time = time.time() filtered_array_snack = tst.loop(array) end_time = time.time() c = round(end_time - start_time, 5) # Creating the table format to the report if a > b and a > c: d = "random_array" elif b > a and b > c: d = "snake_loop" else: d = "loop" t = "Measurement " + str(i+1) column = t, a, b, c, d total_time.append(column) if compare: return total_time else: Write_Results_To_File(filename, function_names, total_time)
import cProfile import io import test_slow_rectangle as t # f = open("cProfile_report.txt", "w") mycode = ''' arr = t.random_array(1e5) t.snake_loop(arr) ''' filename = "cProfile_report.txt" pr = cProfile.Profile() res = pr.run(mycode) res.dump_stats(filename) # res.print_stats() # # f.write(res.print_stats()) # f.close() # TODO: compare cProfile to timeit and manual time
import test_slow_rectangle as tsr import timeit as tit import numpy as np file = open("timeit_reportit.txt", "w") for i in range(3): start_random_array = tit.default_timer() array = tsr.random_array(1e5) end_random_array = tit.default_timer() # times['random_array']= float(end_random_array - start_random_array) start_snake_loop = tit.default_timer() filtered_array = tsr.snake_loop(array) end_snake_loop = tit.default_timer() # times['snake_loop']= float(end_snake_loop - start_snake_loop) start_snack_loop = tit.default_timer() filtered_array_snack = tsr.loop(array) end_snack_loop = tit.default_timer() # times['snack_loop']= float(end_snack_loop - start_snack_loop) times = {'random_array': float(end_random_array - start_random_array), \ 'snake_loop': float(end_snake_loop - start_snake_loop), \ 'snack_loop': float(end_snack_loop - start_snack_loop)} file.write(f"-------- run number {i} ------- \n \n")
print("Method random_array() took: {:.4}s".format(t1 - t0)) f.write("random_array() runtime: {:.4}s\n".format(smallest)) for i in range(3): smallest = float("inf") t0 = time.time() t.loop(arr) t1 = time.time() runtime = t1 - t0 if i == 0: smallest = runtime elif runtime < smallest: smallest = runtime print("Method loop() took: {:.4}s".format(t1 - t0)) f.write("loop() runtime: {:.4}s\n".format(smallest)) for i in range(3): smallest = float("inf") t0 = time.time() t.snake_loop(arr) t1 = time.time() runtime = t1 - t0 if i == 0: smallest = runtime elif runtime < smallest: smallest = runtime print("Method snake_loop() took: {:.4}s".format(t1 - t0)) f.write("snake_loop() runtime: {:.4}s\n".format(smallest)) f.close()
random_array = tsr.random_array(1E5) pr.disable() s = io.StringIO() ps = pstats.Stats(pr, stream=s).sort_stats('tottime') ps.print_stats() print("random_array") with open('cProfile_report.txt', 'w+') as f: f.write("random array \n") f.write(s.getvalue()) pr.enable() filtered_array = tsr.snake_loop(random_array) pr.disable() s = io.StringIO() ps = pstats.Stats(pr, stream=s).sort_stats('tottime') ps.print_stats() print("snake_loop") with open('cProfile_report.txt', 'a') as f: f.write("\n snake_loop \n") f.write(s.getvalue()) pr.enable() filtered_array_snack = tsr.loop(random_array)