Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python on win32: how to get absolute timing / CPU cycle-count

I have a python script that calls a USB-based data-acquisition C# dotnet executable. The main python script does many other things, e.g. it controls a stepper motor. We would like to check the relative timing of various operations, for that purpose the dotnet exe generates a log with timestamps from C# Stopwatch.GetTimestamp(), which as far as I know yields the same number as calls to win32 API QueryPerformanceCounter().

Now I would like to get similar numbers from the python script. time.clock() returns such values, unfortunately it subtracts the value obtained at the time of 1st call to time.clock(). How can I get around this? Is it easy to call QueryPerformanceCounter() from some existing python module or do I have to write my own python extension in C?

I forgot to mention, the python WMI module by Tim Golden does this: wmi.WMI().Win32_PerfRawData_PerfOS_System()[0].Timestamp_PerfTime , but it is too slow, some 48ms overhead. I need something with <=1ms overhead. time.clock() seems to be fast enough, as is c# Stopwatch.GetTimestamp().

TIA, Radim

like image 946
Radim Cernej Avatar asked Feb 25 '23 14:02

Radim Cernej


1 Answers

Have you tried using ctypes?

from ctypes import *
val = c_int64()
windll.Kernel32.QueryPerformanceCounter(byref(val))
print val.value
like image 104
luc Avatar answered Apr 29 '23 20:04

luc