轻松测量经过时间 [英] Easily measure elapsed time
问题描述
我试图使用时间()来衡量不同点我的计划。
我不明白的是,为什么在之前和之后的值是一样的吗?我明白这不是来分析我的计划的最好方式,我只是想看到的东西多长时间。
的printf(** MyProgram ::之前的时间=%LD \\ N,时间(NULL));doSomthing();
doSomthingLong();的printf(** MyProgram ::经过时间=%LD \\ N,时间(NULL));
我曾尝试:
timeval结构差异,startTV,endTV;函数gettimeofday(安培; startTV,NULL);做一点事();
doSomethingLong();函数gettimeofday(安培; endTV,NULL);timersub(安培; endTV,&安培; startTV,&安培;差异);的printf(**花费时间=%LD%LD \\ N,diff.tv_sec,diff.tv_usec);
做我读的结果**如何拍摄时间= 0 26339
?这是否意味着26339纳秒= 26.3毫秒?
什么 **所用的时间= 4 45025
,这是否意味着4秒,25毫秒?
的#include<&的ctime GT;空隙f()的{
使用命名空间std;
clock_t表示开始=时钟(); code_to_time(); clock_t表示结束=时钟();
双elapsed_secs =双(结束 - 开始)/ CLOCKS_PER_SEC;
}
的时间()
功能只精确到秒之内,但也有的 CLOCKS_PER_SEC
一秒钟内的时钟。这是一个简单的,便携式测量,即使它过于简单化了。
I am trying to use time() to measure various points of my program.
What I don't understand is why the values in the before and after are the same? I understand this is not the best way to profile my program, I just want to see how long something take.
printf("**MyProgram::before time= %ld\n", time(NULL));
doSomthing();
doSomthingLong();
printf("**MyProgram::after time= %ld\n", time(NULL));
I have tried:
struct timeval diff, startTV, endTV;
gettimeofday(&startTV, NULL);
doSomething();
doSomethingLong();
gettimeofday(&endTV, NULL);
timersub(&endTV, &startTV, &diff);
printf("**time taken = %ld %ld\n", diff.tv_sec, diff.tv_usec);
How do I read a result of **time taken = 0 26339
? Does that mean 26,339 nanoseconds = 26.3 msec?
What about **time taken = 4 45025
, does that mean 4 seconds and 25 msec?
#include <ctime>
void f() {
using namespace std;
clock_t begin = clock();
code_to_time();
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
}
The time()
function is only accurate to within a second, but there are CLOCKS_PER_SEC
"clocks" within a second. This is an easy, portable measurement, even though it's over-simplified.
这篇关于轻松测量经过时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!