Write a function that calculates the average time taken to perform any transaction by Function fn averaging the total time taken for transaction over Repetations


Topic: Write a function that calculates the average time taken to perform any transaction by Function fn averaging the total time taken for transaction over Repetations

Solution

def time_it(fn, *args, repetitons= 1, **kwargs):
    import time
    total_time = []
    for _ in range(repetitons):
        start_time = time.perf_counter()
        fn(*args,**kwargs)
        end_time = time.perf_counter()
        ins_time = end_time - start_time
        total_time.append(ins_time)
    return sum(total_time)/len(total_time)



List all Python Programs