在C ++中测量CPU时间 [英] Measuring CPU time in c++

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

问题描述

如果我有以下代码

clock_t t;
t = clock();
//algorithm
t = clock() - t;

t等于运行程序的滴答声的数量. CPU时间也一样吗?还有其他方法可以测量C ++中的CPU时间吗?

t would equal the number of ticks to run the program. Is this the same is CPU time? Are there any other ways to measure CPU time in C++?

OS-Debian GNU/Linux 我愿意接受任何可行的方法.我想比较两种算法的CPU时间.

OS -- Debian GNU/Linux I am open to anything that will work. I am wanting to compare the CPU time of two algorithms.

推荐答案

clock()来测量CPU时间,但是并非所有实现都这样做.特别是Microsoft在VS中的实现不会在运行多个线程时计算额外的时间,也不会在程序的线程处于睡眠/等待状态时计算的时间更少.

clock() is specified to measure CPU time however not all implementations do this. In particular Microsoft's implementation in VS does not count additional time when multiple threads are running, or count less time when the program's threads are sleeping/waiting.

还请注意,clock()应该测量整个程序使用的CPU时间,因此,虽然将测量//algorithm中多个线程使用的CPU时间,但不包含在//algorithm中的其他线程也将计算在内.

Also note that clock() should measure the CPU time used by the entire program, so while CPU time used by multiple threads in //algorithm will be measured, other threads that are not part of //algorithm also get counted.

clock()是标准中指定的唯一用于测量CPU时间的方法,但是肯定还有其他特定于平台的测量CPU时间的方法.

clock() is the only method specified in the standard to measure CPU time, however there are certainly other, platform specific, methods for measuring CPU time.

std::chrono不包括任何用于测量CPU时间的时钟.它只有一个与系统时间同步的时钟,一个相对于实时时钟稳定增长的时钟,以及一个高分辨率"的时钟,但不一定测量CPU时间.

std::chrono does not include any clock for measuring CPU time. It only has a clock synchronized to the system time, a clock that advances at a steady rate with respect to real time, and a clock that is 'high resolution' but which does not necessarily measure CPU time.

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

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