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)
else: raise ValueError( "'order' keyword must be 'fast', 'slow' or one of the profiled function's name" ) # Newline for nicer formatting outputs.append("\nTiming performed using: 'time.time'") # If only one function is profiled else: outputs.append("Timing performed using: 'time.time'") # Write results to file with open(filename, "w") as f: f.write("\n".join(outputs)) if __name__ == "__main__": from test_slow_rectangle import loop, random_array, snake_loop n_experiments = 5 array = random_array(1e5) funcs = [random_array, loop, snake_loop] args = [1e5, array, array] timer_results(n_experiments, "manual_report.txt", funcs, args, order="slow")
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")
import time import test_slow_rectangle as t global arr, t0, t1, smallest, runtime f = open("manual_report.txt", "w") for i in range(1, 4): smallest = float("inf") t0 = time.time() arr = t.random_array(1e5) t1 = time.time() runtime = t1 - t0 if i == 0: smallest = runtime elif runtime < smallest: smallest = runtime 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))
import test_slow_rectangle as tsr import numpy as np import cProfile import pstats import io pr = cProfile.Profile() pr.enable() 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")