-1

I want a piece of code that stops exactly in one second
note using time.sleep() does not do the job precisely

here is what I have so far(that doesn't give me precise result)

import time
import sys


start_time = time.time()

time.sleep(.99483-(time.time() - start_time))
print(time.time() - start_time)
sys.exit(0)

also note that the final time should include the last line which is sys.exit(0) execution
I appreciate any help or advice you could spear

cloner
  • 1
  • 2
  • How precise it should be? Milliseconds, microseconds, tens of nanoseconds? – Jerzy Pawlikowski Aug 11 '22 at 16:12
  • @JerzyPawlikowski as precise as it can get – cloner Aug 11 '22 at 16:13
  • You expect microsecond [precision](https://stackoverflow.com/q/9006596/14277722) when measuring with `time`? Please include the code to measure the time you're interested in. – Michael Szczesny Aug 11 '22 at 16:26
  • @MichaelSzczesny I don't think I quite got what you've said I want a way to optimize the code above so that it terminates at exactly one second I also measured it with that print line in the code with python but it also gives me inaccurate results no matter what – cloner Aug 11 '22 at 16:38

1 Answers1

0
import time
import sys


start_time = time.time()
while time.time() - start_time < 1:
    pass
print(time.time() - start_time)

Try this and let me know if this meets your requirement

Akash garg
  • 125
  • 6
  • thanks for answer I've tried that solution before and it gives a result of slightly bigger than 1 but I want it to give something like 0.99999... as precise as it could be – cloner Aug 12 '22 at 10:28