Lokesh
Lokesh

Reputation: 11

if there is a infinite loop in a python code, is it harmful or use large amount of resources?

i am trying to make an alarm clock. this uses the time function and check if current time is equal to the alarm time in a while True loop. does it use heavy amount of resources as it check the condition at the maximum frequency it can. is there a way to reduce this frequency so that less processing load is applied to the processor.

Upvotes: 1

Views: 119

Answers (1)

BrokenBenchmark
BrokenBenchmark

Reputation: 19252

Yes, use time.sleep(). The phenomenon that you're (correctly) trying to avoid is called busy-waiting.

For example, if you wanted to set a ten second delay, you wouldn't want to do:

import time

start = time.time()
while time.time() - start <= 10:
    pass

Instead, you'd want to do:

import time
time.sleep(10)

Upvotes: 2

Related Questions