如何获取进程使用的CPU周期数 [英] How to get the number of CPU cycles used by a process

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

问题描述

我需要使用C#(或VB.Net)来获取特定进程使用的CPU周期数. Sysinternal的Process Explorer中的Process properties弹出窗口中提供了此信息.例如,我正在使用该消息的浏览器当前使用了18,521,360,165赛勒斯(给定或花费几亿).有谁知道如何从.Net应用程序获取此信息?我知道如何获取CPU使用率(百分比),但这不是我想要的.我需要一种方法来比较在不同时间运行的两个不同进程之间的CPU使用率.

I have a need to get the number of CPU cycles used by a specific process using C# (or VB.Net). This information is available in the Process properties popup within Sysinternal's Process Explorer. For instance, the browser that I'm using the post this message has currently used 18,521,360,165 cyles (give or take a few hundred million). Does anyone know how to get this information from a .Net app? I know how to get the CPU usage (percentage), but this isn't what I'm looking for. I need a way to compare CPU usage between two different processes running at different times.

谢谢你, 马特

更新:
我为什么需要这个?我是本地.Net用户组的负责人,我们正在进行代码挑战,开发人员在其中提交代码以解决问题.我需要一种方法来衡量一个条目相对于另一个条目的性能.目前,我正在使用计时器来衡量效果.服务器100%专用于此,但这并不能保证可能同时发生其他事情.显然,这充满了各种潜在问题,但总的来说,这是相当准确的.测量使用的CPU周期数将是一种几乎愚蠢的方法,可以用来衡量某人的条目相对于另一个条目的表现如何.我敢肯定,有人可以在这方面开洞-无需尝试. ;-)我希望这有助于解释我的问题背后的原因以及为什么计时器不足以解决我的问题.

Update:
Why do I need this? I'm the leader of the local .Net user group and we're running a code challenge where developers submit code to solve a problem. I need a way to measure the performance of one entry against another. Currently I'm using a timer to measure performance. The server is 100% dedicated to this, but that doesn't guarantee that something else might be happening at the same time. Obviously, this is frought with all kinds of potential issues, but generally speaking, it's fairly accurate. Measuring the number of CPU cycles used would be an almost fool proof way to measure how well someone's entry performs against another. I'm certain that someone can shoot holes all over this - no need to try at this point. ;-) I hope that helps explain the reason behind my question and why a timer is insufficient for solving my problem.

推荐答案

流程浏览器调用 QueryProcessCycleTime 即可获取该信息.您可能必须使用P/Invoke来调用它.我希望它的P/Invoke签名看起来像:

Process Explorer calls QueryProcessCycleTime to get that information. You will probably have to use P/Invoke to call it. I would expect its P/Invoke signature to look like:

[DllImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool QueryProcessCycleTime(IntPtr ProcessHandle, out ulong CycleTime);

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

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