9951 explained code solutions for 126 technologies


pythonHow to measure time of execution


import time
start_time = time.time()
# code to measure goes here
print("--- %s seconds ---" % (time.time() - start_time))ctrl + c
import time

module to manipulate time

start_time

Saves the start time

time.time()

start_time - calculates delta between current time and time before code execution

"--- %s seconds ---" %

formats execution time


Usage example

import time, random
start_time = time.time()
time.sleep(random.random())
print("--- %s seconds ---" % (time.time() - start_time))
output
--- 0.8802111148834229 seconds ---