How should I measure number of seconds passed in a infinite loop? I'm using Vpython, and in my loop i change a position of some elements etc.
The thing is that i have to change color of lights(sphere) every X seconds and it all happens in infinite loop.
while True:
#some stuff
'here i have to count those seconds'
#some other stuff
You could determine the elapsed time by calling time.time(), and taking the difference.
i have to change color of lights(sphere) every X seconds
Say you last changed the colour at time T. Keep calling time.time() inside the loop, and change the colour again when time.time() - T exceeds X.
You should probably use timeit.default_timer() to figure out what time it is and compute the difference.
e.g.:
from timeit import default_timer
clock_start = default_timer()
#... do something here
clock_end = default_timer()
print "This took %s seconds" %( clock_end - clock_start )
Related
I'm trying to make a program with a cycle that ends as soon as sixty seconds have passed but I don't have the slightest idea of how to do so, any ideas?
You can use the time module to get the system time before the execution of the loop, and then make the loop condition so that it stops when 60 seconds have passed.
import time
seconds = 60
start_time = time.time()
while (time.time() - start_time) < seconds:
print("hello !")
How can I convert the execution time to milliseconds.
I already multiplied the start and end time to 1000.
I used time.time()
Execution Time
Result:
('Start time: ', 1596465418538.365)
Remove.IntNonIdUniqueIndex
('End time: ', 1596465418538.399)
('Execution time: ', 3.409385681152344e-05)
time.time() basic unit is second. It's enough to multiply the difference between end and start by 1000 to get the milliseconds.
import time
start = time.time()
time.sleep(1)
end = time.time()
d = end - start
print(f'executed in {d} seconds or {d*1000} milliseconds')
executed in 1.003673791885376 seconds or 1003.673791885376 milliseconds
If you are measuring program execution times, it is best to use time.monotonic() or time.monotonic_ns().
These functions are guaranteed never to go backwards even in the event of system clock updates.
The first returns a value in seconds, the second in nanoseconds.
I'm creating a loop which executes every 5 seconds, starting at the startTime variable and ending at the stopTime variable. However, the code below is disregarding the minutes within my startTime and endTime variables and only executing on the hour. For example, even though my startTime is '1130', the code is executing 11:05, rather than ending the loop. I have the same problem with the endTime variable. If the current time is 12:45, the code still executes even though the endTime variable is '1230'. The code will stop executing at '1300'.
frequency = 5
startTime = '1130'
endTime = '1230'
while True:
now = datetime.now().strftime('%H:%M:%S')
if startTime <= now <= endTime:
print('Loop is working. Time is: ',now)
time.sleep(frequency)
else:
print('Loop is stopped')
break
I live in Central Time, so I tried switching to Eastern timezone by modifying the "now" variable to:
now = datetime.now(timezone('US/Eastern')).strftime('%H:%M:%S.%f %Z')
but I still get the same problem when I substitute eastern times with startTime and endTime when using the eastern datetime.now().
Is executing code at a precise minute possible with strftime()?
EDIT: (this is now the answer to the real question (oops))
If you want to wait till for example 11:30 (which was the real question)
you can calculate the time (in seconds) the program should sleep (and let it sleep for that time):
def wait_till(hour, minute, second=0):
# get system time (and date)
now_time = datetime.datetime.now()
# create time point we are waiting for (this year, this month and this day)
wait_till_time = datetime.datetime(year=now_time.year, month=now_time.month, day=now_time.day, hour=hour, minute=minute, second=second)
# calculate time we want to wait for and convert to seconds
wait_for = (wait_till_time - now_time).total_seconds()
# check if it's going to be tomorrow (if we would sleep for a negative amount of seconds)
if wait_for < 0:
# add one day
wait_till_time = wait_till_time.replace(day=now_time.day+1)
# recalculate (not very beautiful, but i don't know a better way)
wait_for = (wait_till_time - now_time).total_seconds()
# printing this waiting time (in seconds)
print("waiting for",wait_for,"seconds")
# sleeping for that time
time.sleep(wait_for)
# printing the new now time, so we can see how accurate it is
print("its now",datetime.datetime.now())
and say for example:
wait_till(20, 24) # waiting till 20:24 (today)
and get:
waiting for 15.32297 seconds
its now 2019-03-11 20:24:00.003857
which is pretty darn close to what we wanted (20:24:00.000000) and this delay is probably only caused by the calculation lag of formatting the string.
(The old stuff ...)
if it's not important that it takes 100% 5s (but rather 100.04546642303467% --> it will get off a little bit every time) you can just do
import time
frequency = 5 #every 5 seconds
start_time = time.time()
while 1:
elspsed_time = time.time() - start_time
print(elspsed_time)
time.sleep(frequency)
but if you need the 100% you can try this autocorrecting solution:
import time
from threading import Timer
frequency = 5 #every 5 seconds
start_time = time.time()
def what_to_do_after_5s():
elapsed_time = time.time() - start_time
print(elapsed_time)
# next call
Timer(5.0 - (elapsed_time - int(elapsed_time)), what_to_do_after_5s, ()).start()
what_to_do_after_5s()
and we can see that it autocorrects:
0.0
5.000170707702637
10.000272989273071
15.000539064407349
20.001248836517334
25.00046443939209
30.000929355621338
35.00142860412598
40.0007688999176
45.00128436088562
50.00045442581177
55.000683069229126
60.00123882293701
65.00095415115356
70.0015127658844
I'm trying to make a simple timer which prints the time remaining every second.
for k in range(100):
print(100-k)
t.sleep(1)
#output
#100
#99
#98
#...
#1
However, this will take slightly longer than 100 seconds, because there will be a delay added when print() is used. For long periods, this is slightly noticeable. Is there a way to account for this, and accurately display the time every second? I know I could just sleep(100), but this wouldn't let the time left be printed.
import time
start_time=time.time()
for k in range(25):
print(25-k)
time.sleep(1)
print("it took "+str(float(time.time()-start_time)*1000)+" Milliseconds")
the output with print is: it took 26412.75382041931 Milliseconds
the output without print : it took 25053.035020828247 Milliseconds
it should have been just 25000 milliseconds but it is not
printing will take time, even reading the code takes time
point is don't expect accurate timing with time.sleep() !!!
You can use time.time() to measure elapsed time.
import time
start_time = time.time()
for k in range(100):
# k seconds SHOULD be elapsed at this point
print(100 - k)
slept_time = time.time() - start_time
time.sleep(1 + k-slept_time)
Using time.sleep will never give you the accurate time for your timer, since the time it takes is the one second sleep time + printing time, you can use threading.Timer to get more accurate results. https://repl.it/Hwkt :
import threading, time
start_time=time.time()
def count_loop(counter):
if counter <= 0:
print("it took "+str(float(time.time()-start_time)*1000)+" Milliseconds")
return
threading.Timer(1.0, count_loop, args=[counter-1]).start()
print(counter)
count_loop(100)
This is still not accurate, but with only very minimum offset, only 45 ms. However, when using time.sleep from legendisback's example, there is apparently 81 ms delay. https://repl.it/HwlK
In an MMO game client, I need to create a loop that will loop 30 times in 30 seconds (1 time every second).
To my greatest disappointment, I discovered that I can not use time.sleep() inside the loop because that causes the game to freeze during the loop.
The loop itself is pretty simple and the only difficulty is how to delay it.
limit = 31
while limit > 0 :
print "%s seconds remaining" % (limit)
limit = limit -1
The python libs exist in the client as .pyc files in a separate folder and I'm hoping that I can avoid messing with them.
Do you think that there is any way to accomplish this delay or is it a dead end?
Your game has a main loop. (Yes, it does.)
Each time through the loop when you go to check state, move the players, redraw the screen, etc., you check the time left on your timer. If at least 1 second has elapsed, you print out your "seconds remaining" quip. If At least 30 seconds has elapsed, you trigger whatever your action is.
You can't do it without blocking or threading unless you are willing to lose precision...
I'd suggest sometime like this, but threading is the correct way to do this...
import time
counter = 31
start = time.time()
while True:
### Do other stuff, it won't be blocked
time.sleep(0.1)
print "looping..."
### When 1 sec or more has elapsed...
if time.time() - start > 1:
start = time.time()
counter = counter - 1
### This will be updated once per second
print "%s seconds remaining" % counter
### Countdown finished, ending loop
if counter <= 0:
break
or even...
import time
max = 31
start = time.time()
while True:
### Do other stuff, it won't be blocked
time.sleep(0.1)
print "looping..."
### This will be updated every loop
remaining = max + start - time.time()
print "%s seconds remaining" % int(remaining)
### Countdown finished, ending loop
if remaining <= 0:
break