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

标签 python winapi api performancecounter

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

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

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

TIA, 辐射

最佳答案

您尝试过使用 ctypes 吗?

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

关于python 在 win32 : how to get absolute timing/CPU cycle-count,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4430227/

相关文章:

php - 创建 PHP API : Checking from which server the API Request comes from

Python UDP 客户端临时接收端口

Python 扭曲 react 器 undefined variable

Python if 语法 3.2.2

python - timeit 结果中运行次数和循环次数之间的差异

c++ - 在mfc中获取进程所有者名称

c++ - DeviceContext (HDC) 中的 Alpha channel

windows - 文件大小 - 磁盘上实际使用的字节数(未分配)

api - 谷歌地图 : "This API project is not authorized to use this API."

javascript - 将 JSON 值传递到 POST 请求 - Node