Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Millisecond precise python timer

Tags:

python

timer

I work on a python script designed to control multiple actuator at the same time. To simplify, let's say I need to control 2 electric motors.

With the multiprocessing module I create a process for each motors, and a process to save the data on a sheet.

This part of the script works fine, however I need to command my motors at precise times, every milliseconds, and the time.time() or the time.clock() functions seems to be unreliable (triggs between 0.05 and 30 millisecond!)

Is it "normal" for these functions to be so erratic, or is this caused by another part of my script?

EDIT: I used the datetime function (see below) to improve the precision, but I still have several discrete level of error. For example, if I want 1ms, I get also 1.25, 0.75,1.5... So IMO this is due to computer hardware(as Serge Ballesta said).

like image 805
CoMartel Avatar asked Jul 15 '14 08:07

CoMartel


People also ask

How does Python measure time in milliseconds?

Get the difference between the start_time and end_time by subtracting the two values. Get the total seconds of the time difference by using the syntax time_diff. total_seconds() , where time_diff is the previous result. Convert the previous result to milliseconds by multiplying it by 1000 .

How does Python calculate precise time?

To measure time with high precision, either use time. clock() or time. time() functions. The python docs state that this function should be used for benchmarking purposes.

How do you show milliseconds in Python?

To get the current time in milliseconds in Python: Import time module. Get the current epoch time. Multiply the current epoch time by 1000.

How do I convert milliseconds to time in Python?

Using timedelta. A simple solution is to get the timedelta object by finding the difference of the given datetime with Epoch time, i.e., midnight 1 January 1970. To obtain time in milliseconds, you can use the timedelta. total_seconds() * 1000 .


2 Answers

As I "only" need a relative time (1ms between each command), do you know a way to do that precisely?

The best you can hope for is datetime.datetime.now(), which will give you microsecond resolution:

>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2014, 7, 15, 14, 31, 1, 521368)
>>> i = datetime.datetime.now()
>>> q = datetime.datetime.now()
>>> (i-q).seconds
86389
>>> (i-q).microseconds
648299
>>> i.microsecond
513160
like image 87
Burhan Khalid Avatar answered Sep 19 '22 02:09

Burhan Khalid


IMHO the problem is not in your script but more probably in your machine. I assume you are using a standard computer under Linux or Windows. And even if the computer is able to do many things in one single milliseconds, it constantly does with :

  • network
  • mouse or keyboard events
  • screen (including screen savers ...)
  • antivirus software (mainly on Windows)
  • system or daemon processes (and there are plenty of them)
  • multi-task management

You cannot reliably hope to have a one millisecond accuracy without a dedicated hardware equipement or you must use a real time system.

Edit:

As a complement, here is a quote from an answer from Ulrich Eckhardt to this other post Inconsistent Python Performance on Windows :

You have code that performs serial IO, which will block your process and possibly invoke the scheduler. If that happens, it will first give control to a different process and only when that one yield or exceeds its timeslice it will re-schedule your process.

The question for you is: What is the size of the scheduler timeslice of the systems you are running? I believe that this will give you an insight into what is happening.

like image 26
Serge Ballesta Avatar answered Sep 18 '22 02:09

Serge Ballesta