Use decorator to log time when running an algorithm as follows.

I usually records ms units

1
2
3
4
5
6
7
8
9
import time
def logging_time(original_fn):
    def wrapper_fn(*args, **kwargs):
        start_time = time.time()
        result = original_fn(*args, **kwargs)
        elapsed_time = (time.time() - start_time) * 1e3
        print("WorkingTime[{}]: {:.5f} ms".format(original_fn.__name__, elapsed_time))
        return result
    return wrapper_fn

Therefore, we can use this decorator as follows.

1
2
3
@logging_time
def algorithm():
	...

Tags:

Categories:

Updated:

Leave a comment