Skip to content Skip to sidebar Skip to footer

Measuring The Response Time Between Tasks

I'm coding an program (in Python) that returns me some data. I want to know how measure the response time between the request and the answer (for performance analysis), then I'll s

Solution 1:

Easiest solution would be to write a decorator that does the same.

import time

defcompute_time(func):
     defwrapper(*args, **kwargs):
         start = time.time()
         result = func(*args, **kwargs)
         time_taken = time.time() - start

         print("Function {0}: {1} seconds".format(func.func_name, time_taken)) 
         # Or you can place a logger here too.return result

     return wrapper

@compute_timedefadd(x, y):
    return x + y

With that said, if your use case is complex - consider some tailor-made solution like timeit.

Post a Comment for "Measuring The Response Time Between Tasks"