Use the time()
function
#import the time module
import time
#print the number of seconds since the epoch
print(time.time())
The time(
) function in the time module returns the number of seconds since the epoch (January 1st, 1970) as a floating point value. This can be useful in measuring time for various purposes, such as benchmarking, timing algorithm execution, or simply to measure time elapsed since a certain point.
time()
The function takes no argument.
The function returns a floating point value representing the number of seconds since the epoch up to the current moment.
converting the returned seconds into a human-friendly date using the time.ctime()
function
#import the module
import time
now = time.time()
#convert the seconds
current_date = time.ctime(now)
print(current_date)
Use the time() function to measure execution time
Measuring execution time
import time
def delayed_add(a, b):
start = time.time()
result = a + b
#delay the function using the sleep function
time.sleep(1)
end = time.time()
print("Execution took %f seconds"%(end - start))
return f"{a} + {b} = {result}"
print(delayed_add(10, 20))