Visual C ++:如何获取CPU时间?

时间:2022-09-02 09:20:05

How can I programatically find the CPU time which is displayed in System Idle Process (in Task Manager) using Visual C++?

如何使用Visual C ++以编程方式查找系统空闲进程(在任务管理器中)中显示的CPU时间?

5 个解决方案

#1


What you want is something like this...

你想要的是这样的......

NTSTATUS hStatus;
SYSTEM_PERFORMANCE_INFORMATION             stSysPerfInfo;

hStatus = NtQuerySystemInformation(SystemPerformanceInformation, &stSysPerfInfo, sizeof(stSysPerfInfo), NULL);
if (hStatus != NO_ERROR)
{
  // Do work....
}

Or take a look at this "TaskManager"

或者看看这个“TaskManager”

http://reactos.freedoors.org/Reactos%200.3.8/ReactOS-0.3.8-REL-src/base/applications/taskmgr/

#2


I don't have a windows around to really know what the question is, but maybe you can look into the std::clock standard function for measuring spent cpu time. If you request that time twice, the amount of ticks in the elapsed time period can be converted to seconds through the constant CLOCKS_PER_SEC.

我没有窗口可以真正知道问题是什么,但也许你可以查看std :: clock标准函数来测量花费的cpu时间。如果您请求该时间两次,则可以通过常量CLOCKS_PER_SEC将经过时间段中的滴答数量转换为秒。

The result will be the CPU time your process has spent, that will differ from a wall clock. It can be higher in multithreaded apps, or lower if your code _sleep_s, as it will not be spending time.

结果将是您的过程花费的CPU时间,这将与挂钟不同。它可以在多线程应用程序中更高,如果你的代码_sleep_s可以更低,因为它不会花费时间。

void f()
{
   std::clock_t init = std::clock();
   // perform some operations
   std::clock_t end = std::clock();
   std::cout << end-init << " cpu ticks spent, or about " 
             << (end-init)/CLOCKS_PER_SEC << " seconds." << std::endl;
}

This will not count for the CPU time before the first measurement, but it can give you a close measurement in an standard way.

这不会计入第一次测量之前的CPU时间,但它可以以标准方式为您提供近距离测量。

#3


Never tried it, but GetProcessTimes seems to be the way to go.

从来没有尝试过,但GetProcessTimes似乎是要走的路。

#4


I'd use the WMI Performance Counters, specifically Process/%ProcessorTime/Idle.

我使用WMI性能计数器,特别是Process /%ProcessorTime / Idle。

Read this Article for how to do it in C# http://www.codeproject.com/KB/dotnet/perfcounter.aspx

阅读本文,了解如何在C#http://www.codeproject.com/KB/dotnet/perfcounter.aspx中执行此操作

and in C++: http://msdn.microsoft.com/en-us/library/aa394558(VS.85).aspx

在C ++中:http://msdn.microsoft.com/en-us/library/aa394558(VS.85).aspx

Hope this answers your question.

希望这能回答你的问题。

#5


Also see my post on this: http://osequal.blogspot.com/2009/03/accurate-time-measurement-for.html . In particular, check the implementation of tick_count in Intel TBB library: http://cc.in2p3.fr/doc/INTEL/tbb/doc/html/a00199.html

另见我的帖子:http://osequal.blogspot.com/2009/03/accurate-time-measurement-for.html。特别是,检查英特尔TBB库中tick_count的实现:http://cc.in2p3.fr/doc/INTEL/tbb/doc/html/a00199.html

#1


What you want is something like this...

你想要的是这样的......

NTSTATUS hStatus;
SYSTEM_PERFORMANCE_INFORMATION             stSysPerfInfo;

hStatus = NtQuerySystemInformation(SystemPerformanceInformation, &stSysPerfInfo, sizeof(stSysPerfInfo), NULL);
if (hStatus != NO_ERROR)
{
  // Do work....
}

Or take a look at this "TaskManager"

或者看看这个“TaskManager”

http://reactos.freedoors.org/Reactos%200.3.8/ReactOS-0.3.8-REL-src/base/applications/taskmgr/

#2


I don't have a windows around to really know what the question is, but maybe you can look into the std::clock standard function for measuring spent cpu time. If you request that time twice, the amount of ticks in the elapsed time period can be converted to seconds through the constant CLOCKS_PER_SEC.

我没有窗口可以真正知道问题是什么,但也许你可以查看std :: clock标准函数来测量花费的cpu时间。如果您请求该时间两次,则可以通过常量CLOCKS_PER_SEC将经过时间段中的滴答数量转换为秒。

The result will be the CPU time your process has spent, that will differ from a wall clock. It can be higher in multithreaded apps, or lower if your code _sleep_s, as it will not be spending time.

结果将是您的过程花费的CPU时间,这将与挂钟不同。它可以在多线程应用程序中更高,如果你的代码_sleep_s可以更低,因为它不会花费时间。

void f()
{
   std::clock_t init = std::clock();
   // perform some operations
   std::clock_t end = std::clock();
   std::cout << end-init << " cpu ticks spent, or about " 
             << (end-init)/CLOCKS_PER_SEC << " seconds." << std::endl;
}

This will not count for the CPU time before the first measurement, but it can give you a close measurement in an standard way.

这不会计入第一次测量之前的CPU时间,但它可以以标准方式为您提供近距离测量。

#3


Never tried it, but GetProcessTimes seems to be the way to go.

从来没有尝试过,但GetProcessTimes似乎是要走的路。

#4


I'd use the WMI Performance Counters, specifically Process/%ProcessorTime/Idle.

我使用WMI性能计数器,特别是Process /%ProcessorTime / Idle。

Read this Article for how to do it in C# http://www.codeproject.com/KB/dotnet/perfcounter.aspx

阅读本文,了解如何在C#http://www.codeproject.com/KB/dotnet/perfcounter.aspx中执行此操作

and in C++: http://msdn.microsoft.com/en-us/library/aa394558(VS.85).aspx

在C ++中:http://msdn.microsoft.com/en-us/library/aa394558(VS.85).aspx

Hope this answers your question.

希望这能回答你的问题。

#5


Also see my post on this: http://osequal.blogspot.com/2009/03/accurate-time-measurement-for.html . In particular, check the implementation of tick_count in Intel TBB library: http://cc.in2p3.fr/doc/INTEL/tbb/doc/html/a00199.html

另见我的帖子:http://osequal.blogspot.com/2009/03/accurate-time-measurement-for.html。特别是,检查英特尔TBB库中tick_count的实现:http://cc.in2p3.fr/doc/INTEL/tbb/doc/html/a00199.html