win32 上的 python:如何获得绝对时间/CPU 周期计数 [英] python on win32: how to get absolute timing / CPU cycle-count

查看:42
本文介绍了win32 上的 python:如何获得绝对时间/CPU 周期计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 python 脚本,它调用基于 USB 的数据采集 C# dotnet 可执行文件.主要的 python 脚本还做很多其他的事情,例如它控制步进电机.我们想检查各种操作的相对时间,为此,dotnet exe 生成一个带有来自 C# Stopwatch.GetTimestamp() 的时间戳的日志,据我所知,它产生的数量与调用 win32 API QueryPerformanceCounter() 的数量相同.

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().

现在我想从 python 脚本中得到类似的数字.time.clock() 返回这样的值,不幸的是它减去了第一次调用 time.clock() 时获得的值.我怎样才能解决这个问题?从一些现有的 python 模块调用 QueryPerformanceCounter() 是否容易,还是我必须用 C 编写自己的 python 扩展?

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?

我忘了提,Tim Golden 的 python WMI 模块是这样做的:wmi.WMI().Win32_PerfRawData_PerfOS_System()[0].Timestamp_PerfTime,但它太慢了,有一些 48ms 的开销.我需要 <=1ms 开销的东西.time.clock() 似乎足够快,c# Stopwatch.GetTimestamp() 也是如此.

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,半径

推荐答案

您是否尝试过使用 ctypes?

Have you tried using ctypes?

from ctypes import *
val = c_int64()
windll.Kernel32.QueryPerformanceCounter(byref(val))
print val.value

这篇关于win32 上的 python:如何获得绝对时间/CPU 周期计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆