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

查看:133
本文介绍了如何获得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.如Wikipedia文章所述,最好使用

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天全站免登陆