Stressor
Stressor

Reputation: 1

python: endless while loop with fixed frequency

in my programm i have a endless while loop which needs to run in a fixed frequency to give da constant datarate. My idea (which I copied from github) was to get a timestamp outside the loop and inside the loop, and use the delta as my frequency.

import time

time_after_loop = time.process_time() # initalisation
frequency = 0.05

while True:
    time_before_loop = time.process_time()
    if time_before_loop - time_after_loop >= frequency:
        real_frequency = time_before_loop - time_after_loop
        print(real_frequency)
        # main programm
        time_after_loop = time.process_time()

I use windows 10 and python 3.8 and the print values show some interessting behavierous

frequency = 0.5 --> real_frequency = 0.5

frequency = 0.1 --> real_frequency = 0.109375

frequency = 0.05 --> real_frequency = 0.0625

frequency = 0.001 --> real_frequency = 0.015625 --> max frequency

It looks like a systematic error, not a problem in the code? Does someone know what happend here and how to fix it?

Upvotes: 0

Views: 1376

Answers (5)

Stressor
Stressor

Reputation: 1

Thank you very much!! You helped me a lot

time.time()

and

time.perf_counter()

worked well

time.process_time() was the wrong function to use here!

Upvotes: 0

bara-elba
bara-elba

Reputation: 146

try using (time.time()) instead of time.process_time() and see if it gaves you a more accurate results.

import time

  time_after_loop = time.time() # initialization
  frequency = 0.1

  while True:
    time_before_loop = time.time()
    if time_before_loop - time_after_loop >= frequency:
      real_frequency = time_before_loop - time_after_loop
      print(real_frequency)
      # main programm
      time_after_loop = time.time()

Upvotes: 2

GordonAitchJay
GordonAitchJay

Reputation: 4860

Use time.perf_counter() instead of time.process_time(), as it includes time elapsed during sleep and is system-wide.

Return the value (in fractional seconds) of a performance counter, i.e. a clock with the highest available resolution to measure a short duration. It does include time elapsed during sleep and is system-wide.

Output:

0.05000000004656613
0.05000000004656613
0.05000010004732758
0.050000099930912256
0.050000099930912256
0.05001329991500825
0.05000010004732758
0.050000099930912256
0.050000099930912256
0.05000000004656613
0.050000099930912256
0.05000010004732758

Upvotes: 1

Keegan Cowle
Keegan Cowle

Reputation: 2479

Python time is only accurate to +- 0.001 milliseconds for Linux and Mac and +- 0.016 for windows. Getting really really accurate is hard. here for more reading

Upvotes: 0

Daweo
Daweo

Reputation: 36450

It looks like a systematic error, not a problem in the code?

I am not sure if this is case here but please note that print is not free timewise (it takes low but not zero time to print)

Does someone know what happend here and how to fix it?

I suggest looking at threading, Timer might be used for starting execution of action every x seconds regardless of how much time said action is taking, consider following simple example which does endlessly print 2 times second (i.e. every 0.5 seconds)

import threading

def func(): 
    threading.Timer(0.5, func).start()
    print("Hello")

func()

Note that next execution of func is shoved into queue before proper action is taken.

Upvotes: 0

Related Questions