I finally figured this out and would like to share the knowledge and save someone a bunch of time, so see my answer below. However, I still need answers for Linux, so please answer if you know, as my code in my answer is for Windows only.
UPDATE: I've figured it out for Linux too, including for pre-Python 3.3 (ex: for the Raspberry Pi), and I've posted my new module/code in my answer below.
My original question: How do I get millisecond and microsecond-resolution timestamps in Python? I'd also like the Arduino-like delay and delayMicroseconds() functions.
UPDATE 19 Dec. 2018: Please don't mark this question as a duplicate and say it has an answer elsewhere when it definitely does not.
This question was marked as a duplicate of this one a few months ago. See here:
It says, "This question already has an answer here." Unfortunately, that's just not true. I read those answers before asking this question, years ago, and they don't answer my question nor meet my needs. They are just as inapplicable to my question as is the most downvoted answer here, which is greyed out because it is unfortunately wrong since it relies on the time
module, which prior to Python 3.3 did NOT have any type of guaranteed resolution whatsoever:
Please re-open my question. It is not a duplicate. It does not have a prior answer from another question. The question linked as already containing an answer relies on the time
module, and even states its resolution is all over the place. The most upvoted answer there quotes a Windows resolution using their answer of 16 ms, which is 32000 times worse than my answer I provided here (0.5 us resolution). Again, I needed 1 ms and 1 us (or similar) resolutions, NOT 16000 us resolution. Therefore, it is not a duplicate.
Thanks for your time. :)
See also: How can I make a time delay in Python?
For Windows:Here's a fully-functional module for both Linux (works with pre-Python 3.3 too) and Windows:Functions and code samples.
Functions include:
Python code module:
If you know how to get the above millisecond and microsecond-resolution timestamps in Linux, please post, as that would be very helpful too.This works for Linux too, including in pre-Python 3.3, since I'm using C functions via the ctypes module in order to read the time stamps.
(Note: code above originally posted here: http://www.electricrcaircraftguy.com/2016/07/arduino-like-millisecond-and-microsecond-timestamps-in-python.html)
Special thanks to @ArminRonacher for his brilliant pre-Python 3.3 Linux answer here: https://stackoverflow.com/a/1205762/4561887
Update: prior to Python 3.3, the built-in Python time library (https://docs.python.org/3.5/library/time.html) didn't have any explicitly high-resolution functions. Now, however it does provide other options, including some high-resolution functions.
My module above, however, provides high-resolution timestamps for Python code before Python 3.3, as well as after, and it does so on both Linux and Windows.
Here's an example of what I mean, showing that the
time.sleep()
function is NOT necessarily a high-resolution function. *On my Windows machine, it's resolution is perhaps 8ms at best, whereas my module above has 0.5us resolution (16000 times better!) on the same machine.Code demonstration:
SAMPLE RESULTS ON MY WINDOWS 8.1 MACHINE (notice how much worse time.sleep does):
SAMPLE RESULTS ON MY RASPBERRY PI VERSION 1 B+ (notice that the results between using time.sleep and my module are basically identical...apparently the low-level functions in
time
are already accessing better-resolution timers here, since it's a Linux machine (running Raspbian)...BUT in myGS_timing
module I am explicitly calling the CLOCK_MONOTONIC_RAW timer. Who knows what's being used otherwise):