How to use a Python timer to measure elapsed time?

How do I use a python timer to measure elapsed time?

I want to measure the time it took to execute a function. I couldn’t get timeit to work:

import timeit
start = timeit.timeit()
print("hello")
end = timeit.timeit()
print(end - start)

Using time.time(): You can use time.time() to capture the start and end times and calculate the elapsed time.

import time

start_time = time.time() print(“hello”) end_time = time.time()

print(f"Elapsed time: {end_time - start_time} seconds")

This method uses the time.time() function to get the current time in seconds since the epoch and measures the difference.