网站首页 > 基础教程 正文
我们可以通过 时间度量 - Wall time vs. CPU time 来知道Wall time和CPU time的区别是什么,简单来讲,Wall Time就是类似我们的时钟一样,他没有很精确的表示此时CPU花了多少时间,而是直接用了比较粗的方式去统计。而CPU Time就比较精确的告诉了我们,CPU在执行这段指令的时候,一定消耗了多少时间。接下来我们将简单介绍8种方式来进行程序计时
如果你本身的测试程序是一个高密度的计算,比如while (10000) 内部疯狂做计算,那么你的CPU占用肯定是100%,这种情况下,你的wall time和你的CPU time其实很难区分。在这种情况下,如果你想让你的CPU idle一会的话,你可以简单的通过sleep()就可以进行实现,因为CPU在sleep()状态下是IDLE的。
1.time命令 - for Linux(Windows没有找到合适的替代品), Wall Time + CPU Time, seconds
你可以直接time你的program, 而且不需要修改你的代码,当你的程序结束运行,对应的结果会刷出来,他会同时包含wall time以及CPU time
上面的real表示wall time, user表示CPU time.同时你不需要修改你的代码。
2.#include <chrono> - for Linux & Windows(需要C++ 11),Wall Time, nanoseconds
他是度量wall time的最合适和可移植性的方法。chrono拥有你机器中的不同的clocks, 每个clock都有各自不同的目的和特征。当然除非你有特殊的要求,一般情况下你只需要high_resolution_clock.他拥有最高精度的clock,本身也应该比较实用
#include <stdio.h>
#include <chrono>
int main () {
double sum = 0;
double add = 1;
// Start measuring time
auto begin = std::chrono::high_resolution_clock::now();
int iterations = 1000*1000*1000;
for (int i=0; i<iterations; i++) {
sum += add;
add /= 2.0;
}
// Stop measuring time and calculate the elapsed time
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
printf("Result: %.20f\n", sum);
printf("Time measured: %.3f seconds.\n", elapsed.count() * 1e-9);
return 0;
}
可以看到,通过duration_cast我们可以把时间转换到nanoseconds,除此之外,还支持hours, minutes, seconds, millseconds, microseconds。同时需要注意的是,使用chrono库可能会比其他的C/C++方法需要损失的性能更高,尤其是在一个loop执行多次。
3.#include <sys/time.h> gettimeofday() - for Linux & Windows, Wall time,microseconds
这个函数会返回从00:00:00 UTC(1970 1.1) Epoch time. 比较tricky的一点是,这个函数会同时返回 seconds以及microseconds在不同的分别的long int变量里。所以,如果你想获取总的时间包括microseconds,你需要手动对他们做sum()
#include <stdio.h>
#include <sys/time.h>
int main () {
double sum = 0;
double add = 1;
// Start measuring time
struct timeval begin, end;
gettimeofday(&begin, 0);
int iterations = 1000*1000*1000;
for (int i=0; i<iterations; i++) {
sum += add;
add /= 2.0;
}
// Stop measuring time and calculate the elapsed time
gettimeofday(&end, 0);
long seconds = end.tv_sec - begin.tv_sec;
long microseconds = end.tv_usec - begin.tv_usec;
double elapsed = seconds + microseconds*1e-6;
printf("Result: %.20f\n", sum);
printf("Time measured: %.3f seconds.\n", elapsed);
return 0;
}
- 如果你对小数点不感冒,你可以直接通过end.tv_sec - begin.tv_sec来获取秒级单位
- 第二个参数是用来设置当前的timezone.由于我们计算的是elapsed time, 因此timezone就没有关系
4.#include <time.h> time() - for Linux & Windows, Wall time, seconds
time()跟第三条的gettimeofday()类似,他会基于Epoch time。但是又跟gettimeofday()有两个比较大的区别:
- 你不能像gettimeofday()一样在第二个参数设置timezone,所以他始终是UTC
- 他始终只会返回full seconds
因此因为上面的第二点原因,除非你的测量精度就是seconds,否则意义不大
#include <stdio.h>
#include <time.h>
int main () {
double sum = 0;
double add = 1;
// Start measuring time
time_t begin, end;
time(&begin);
int iterations = 1000*1000*1000;
for (int i=0; i<iterations; i++) {
sum += add;
add /= 2.0;
}
// Stop measuring time and calculate the elapsed time
time(&end);
time_t elapsed = end - begin;
printf("Result: %.20f\n", sum);
printf("Time measured: %ld seconds.\n", elapsed);
return 0;
}
time_t实际上就是alias long int,因此你可以直接用printf()和cout进行打印
5.#include <time.h> clock() - for Linux & Windows, CPU time on Linux, Wall time on Windows, seconds
clock()会返回在程序开始执行之后的clock ticks. 你可以通过他去除以CLOCKS_PER_SEC(在自己的Linux虚拟机测下来这个值是1000000),你会得到程序执行了多少时间(s).但是这对于不同的操作系统,行为是不一样的,比如在Linux,你得到的是CPU time,在Windows,你得到的是Wall time.因此你需要特别小心
#include <stdio.h>
#include <time.h>
int main () {
double sum = 0;
double add = 1;
// Start measuring time
clock_t start = clock();
int iterations = 1000*1000*1000;
for (int i=0; i<iterations; i++) {
sum += add;
add /= 2.0;
}
// Stop measuring time and calculate the elapsed time
clock_t end = clock();
double elapsed = double(end - start)/CLOCKS_PER_SEC;
printf("Result: %.20f\n", sum);
printf("Time measured: %.3f seconds.\n", elapsed);
return 0;
}
clock_t本身也是long int, 所以当你用除法去除以CLOCKS_PER_SEC的时候,你需要先转换到double,否则你会因为整除而失去信息
6.#include <time.h> clock_gettime() - for Linux Only, Wall time / CPU time,nanoseconds
这个API是比较强大的,因为他既可以用来测Wall time又可以用来测CPU time,同时他支持nanoseconds级别。但是他唯一的不好的地方就是他只适用于Unix.你可以通过flag来控制测量wall time或者是CPU time,通过对应的CLOCK_PROCESS_CPUTIME_ID(wall time)或者CLOCK_REALTIME(CPU time)
#include <stdio.h>
#include <time.h>
int main () {
double sum = 0;
double add = 1;
// Start measuring time
struct timespec begin, end;
clock_gettime(CLOCK_REALTIME, &begin);
int iterations = 1000*1000*1000;
for (int i=0; i<iterations; i++) {
sum += add;
add /= 2.0;
}
// Stop measuring time and calculate the elapsed time
clock_gettime(CLOCK_REALTIME, &end);
long seconds = end.tv_sec - begin.tv_sec;
long nanoseconds = end.tv_nsec - begin.tv_nsec;
double elapsed = seconds + nanoseconds*1e-9;
printf("Result: %.20f\n", sum);
printf("Time measured: %.3f seconds.\n", elapsed);
return 0;
}
除了CLOCK_REALTIME和CLOCK_PROCESS_CPUTIME_ID之外,你可以使用其他的clock types. 这里的timespec跟gettimeofday()的timeval很类似,但是timeval包含的是microseconds,而timespec包含的是nanoseconds
7.#include <sysinfoapi.h> GetTickCount64() - for Windows Only, milliseconds
返回当系统开机之后的millseconds毫秒个数,同样这里也有32位的版本GetTickCount(), 但是他会把时间限制在49.71 days,所以用64 bit都是比较好的
#include <stdio.h>
#include <sysinfoapi.h>
int main () {
double sum = 0;
double add = 1;
// Start measuring time
long long int begin = GetTickCount64();
int iterations = 1000*1000*1000;
for (int i=0; i<iterations; i++) {
sum += add;
add /= 2.0;
}
// Stop measuring time and calculate the elapsed time
long long int end = GetTickCount64();
double elapsed = (end - begin)*1e-3;
printf("Result: %.20f\n", sum);
printf("Time measured: %.3f seconds.\n", elapsed);
return 0;
}
8.#include <processthreadsapi.h> GetProcessTimes() - for Windows Only(但是这时唯一可以在Windows上测量CPU time的方法), CPU time
原理本身比较复杂,如果有兴趣可以自己找资料继续阅读
#include <stdio.h>
#include <processthreadsapi.h>
double get_cpu_time(){
FILETIME a,b,c,d;
if (GetProcessTimes(GetCurrentProcess(),&a,&b,&c,&d) != 0){
// Returns total user time.
// Can be tweaked to include kernel times as well.
return
(double)(d.dwLowDateTime |
((unsigned long long)d.dwHighDateTime << 32)) * 0.0000001;
}else{
// Handle error
return 0;
}
}
int main () {
double sum = 0;
double add = 1;
// Start measuring time
double begin = get_cpu_time();
int iterations = 1000*1000*1000;
for (int i=0; i<iterations; i++) {
sum += add;
add /= 2.0;
}
// Stop measuring time and calculate the elapsed time
double end = get_cpu_time();
double elapsed = (end - begin);
printf("Result: %.20f\n", sum);
printf("Time measured: %.3f seconds.\n", elapsed);
return 0;
}
网上有人基于这个工具提供了同时计算CPU time以及wall time的方法, 有兴趣的话可以自己去进行进一步学习
猜你喜欢
- 2024-10-19 Python3 datetime模块指南:日期时间操作、时区管理与实战案例
- 2024-10-19 C++及数据结构复习笔记,类和对象很简单?为啥好多程序员还不会
- 2024-10-19 Linux时间和日期 linux 时间 表示方法
- 2024-10-19 Python实战:使用 datetime模块处理时间日期的全方位指南
- 2024-10-19 C++编程的 42 条建议(四) c++编程100例
- 2024-10-19 mount with noatime - 合理关闭atime提高服务器性能
- 2024-10-19 linux下连续三次fork()——深度理解进程创建函数
- 2024-10-19 C函数time和clock的计时区别 c计时器函数
- 2024-10-19 win进程弹出Microsoft Visual C++ Runtime Library的解决办法
- 2024-10-19 库函数——ctime()和ctime不安全函数
- 最近发表
- 标签列表
-
- gitpush (61)
- pythonif (68)
- location.href (57)
- tail-f (57)
- pythonifelse (59)
- deletesql (62)
- c++模板 (62)
- css3动画 (57)
- c#event (59)
- linuxgzip (68)
- 字符串连接 (73)
- nginx配置文件详解 (61)
- html标签 (69)
- c++初始化列表 (64)
- exec命令 (59)
- canvasfilltext (58)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- node教程 (59)
- console.table (62)
- c++time_t (58)
- phpcookie (58)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)