(先学c还是直接学c+)(学c之前先学什么)

(先学c还是直接学c+)(学c之前先学什么)

我们可以通过 时间度量 - 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

(先学c还是直接学c+)(学c之前先学什么)

上面的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的方法, 有兴趣的话可以自己去进行进一步学习

声明:我要去上班所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流,版权归原作者编编成程所有,原文出处。若您的权利被侵害,请联系删除。

本文标题:(先学c还是直接学c+)(学c之前先学什么)
本文链接:https://www.51qsb.cn/article/m8y8n.html

(0)
打赏微信扫一扫微信扫一扫QQ扫一扫QQ扫一扫
上一篇2023-03-08
下一篇2023-03-09

你可能还想知道

发表回复

登录后才能评论