C ++中进程的时间消耗 [英] Time consumption of a process in C++

查看:109
本文介绍了C ++中进程的时间消耗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您认为通过以下代码查找流程的时间消耗是正确的吗?

Do you think finding time consumption of a process by a code below is right?

#include <time.h>
clock_t start, end;
double time;
start = clock();
/*process here*/
end = clock();
time = (double) (end - start)/CLOCKS_PER_SECOND;





我用它而且我有时间消耗一个过程,但我需要验证它是对还是错?如果有任何其他解决方案,PLZ建议我!!



以上代码,如果我们找时间,它会以秒还是毫秒?



thanx !!



I used it and I am getting time consumption of a process, but I need to verify whether it is right or wrong? and if there is any other solution, plz suggest me!!

by above code, if we find time, will it be in sec or msec?

thanx!!

推荐答案

代码计算自启动流程以来的时间(以秒为单位)。



如果您需要Windows线程的执行时间,可以使用 GetThreadTimes() [ ^ ]功能。
The code calculates the time since starting your process in seconds.

If you need the execution times for Windows threads, you can use the GetThreadTimes()[^] function.


您也可以使用GetTickCount这个。



例如。



///示例代码///

DWORD dwStartTime = :: GetTickCount();



///完成流程



DWORD dwDiff = :: GetTickCount() - dwStartTime;

/// /////////// ///



它会给你时间差,以毫秒为单位。



:)
You may use GetTickCount also for this.

eg.

/// EXAMPLE CODE ///
DWORD dwStartTime = ::GetTickCount();

/// Done the process

DWORD dwDiff = ::GetTickCount() - dwStartTime;
/// /////////// ///

It will give you the time difference in milliseconds.

:)


两个时钟() GetTickCount()使用系统计时器,每10-16ms更新一次,具体取决于您的硬件。为了测量一个过程的时间消耗,这个时间分辨率通常是不够的。



更重要的是,这两种方法都不是特定于一个过程 - 它们只是衡量全球时间,而不是进程在CPU上使用的时间!



你应该看看 QueryPerformanceCounter [ ]。如果您按照该链接进行操作,您将找到一个带有高分辨率计时器(HRTimer类)列表的注释,该计时器仅指当前线程的活动时间。
Both clock() and GetTickCount() use the system timer which only gets updated once every 10-16ms, depending on your hardware. For measuring time consumption of a process this time resolution is usually not sufficient.

More importantly, both methods do are not specific to a process - they just measure the global time, not the time a process uses on the CPU!

You should take a look at QueryPerformanceCounter[]. If you follow that link, you''ll find a comment with a listing for a high resolution timer (class HRTimer) that refers only to the active time of the current thread.


这篇关于C ++中进程的时间消耗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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