I'm very new to python, and I'm trying to make a sort of idle game. I'm having a little bit of trouble optimizing a function I wrote that flashes "ON" and then "OFF" every time a second passes.
While it works fine, it checks to see if a second has passed so many times that it peaks my CPU. The code is below:
import time
def timer():
oldtime = int(time.time())
#grabs the time on initiation
while True:
if oldtime < int(time.time()):
return 'ON'
#flashes on
return 'OFF'
#makes everything that checks if it's on only proc once
oldtime += int(time.time()) - oldtime
#makes oldtime advance one second ahead of time.time(),
#even if more than one second passes at a time
timer()
while timer() == "ON":
print('lorem ipsum')
#output: lorem ipsum every second and a peaking CPU
How would one go about optimizing something like this?