1 | initial version |
You can use the time module in Python to determine the duration of a function executing in a loop. Here's an example:
import time
for i in range(10):
start_time = time.time() # Get the start time
# Execute the function here
end_time = time.time() # Get the end time
duration = end_time - start_time # Calculate the duration
if duration > 5: # If the duration is more than 5 seconds
continue # Skip to the next iteration
In this example, the start time is obtained at the beginning of each iteration using the time.time()
function. After the function has executed, the end time is obtained and the duration is calculated by subtracting the start time from the end time. If the duration is greater than 5 seconds, the continue
statement is used to skip to the next iteration of the loop.