在 Win32 中如何获取 CPU 周期数? [英] How do I obtain CPU cycle count in Win32?

查看:21
本文介绍了在 Win32 中如何获取 CPU 周期数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Win32 中,有没有办法获得唯一的 cpu 周期计数或类似的东西,以便对多个进程/语言/系统/等进行统一.

In Win32, is there any way to get a unique cpu cycle count or something similar that would be uniform for multiple processes/languages/systems/etc.

我正在创建一些日志文件,但必须生成多个日志文件,因为我们托管 .NET 运行时,我想避免从一个调用到另一个来记录.因此,我想我只需要生成两个文件,将它们组合起来,然后对它们进行排序,以获得涉及跨世界调用的连贯时间线.

I'm creating some log files, but have to produce multiple logfiles because we're hosting the .NET runtime, and I'd like to avoid calling from one to the other to log. As such, I was thinking I'd just produce two files, combine them, and then sort them, to get a coherent timeline involving cross-world calls.

但是,GetTickCount 不会每次调用都增加,所以这是不可靠的.有没有更好的数字,以便我在排序时按正确的顺序收到电话?

However, GetTickCount does not increase for every call, so that's not reliable. Is there a better number, so that I get the calls in the right order when sorting?

编辑:感谢 @Greg 让我走上了 QueryPerformanceCounter 的轨道, 成功了.

Edit: Thanks to @Greg that put me on the track to QueryPerformanceCounter, which did the trick.

推荐答案

您可以使用 RDTSC CPU 指令(假设 x86).这条指令给出了 CPU 周期计数器,但要注意它会很快增加到它的最大值,然后重置为 0.正如维基百科文章提到的,你最好使用 QueryPerformanceCounter 函数.

You can use the RDTSC CPU instruction (assuming x86). This instruction gives the CPU cycle counter, but be aware that it will increase very quickly to its maximum value, and then reset to 0. As the Wikipedia article mentions, you might be better off using the QueryPerformanceCounter function.

这篇关于在 Win32 中如何获取 CPU 周期数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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