测量时间用C [英] Measuring time in C

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

问题描述

我试图来衡量C(矩阵乘法)的一些活动,并注意到了我应该做这样的事情:

I'm trying to measure some activity in C (Matrix multiplying) and noticed that I should do something like this:

clock_t start = clock();
sleep(3);
clock_t end = clock();
double elapsed_time = (end - start)/(double)CLOCKS_PER_SEC;
printf("Elapsed time: %.2f.\n", elapsed_time);

输出是:

Elapsed time: 0.00.

为什么会出现这种情况?

Why is this happening?

推荐答案

时钟估计你的程序使用的CPU时间;这就是CPU已经属于你的程序忙于执行指令的时间。 睡眠不执行任何工作,因此它没有明显的CPU时间(即使需要挂钟时间)。

clock estimates the CPU time used by your program; that's the time the CPU has been busy executing instructions belonging to your program. sleep doesn't perform any work, so it takes no noticeable CPU time (even if it takes wallclock time).

如果你想测量时钟时间,使用时间

If you want to measure wallclock time, use time:

time_t start = time(NULL);
sleep(3);
printf("%.2f\n", (double)(time(NULL) - start));

将关闭打印一个数字为三个。

will print a number close to three.

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

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