I need to run a loop during a precise duration. For example, the code I'm trying to make would look like that :
initialize and lauch timer
while timer<10000sec:
do things
Do you know how to do this ?
Thank you :)
stop = time.time()+10000
while time.time() < stop:
do things
This requires that each run through the loop is sufficiently short so that you read the current time often enough (for the preciseness that you want to achieve).
time.time() on each iteration can dominate the runtime. What I'm trying to say to the OP is that if performance is critical here, he may consider an alternative approachimport time
t_end = time.time() + 60 * <> #duration goes inside <>; eg 2 for 2 minutes
while time.time() < t_end:
#rest of the code
do thingsbe aborted after given time or should it finish first?