如何使用timeval结构,以获得执行时间? [英] How to use struct timeval to get the execution time?

查看:370
本文介绍了如何使用timeval结构,以获得执行时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

了解读关于耗时这篇文章后 ,我写一个简单的code,计算一个循环的执行时间:

After read about this article about elapsed time, I write a simple code to calculate the execution time of a loop:

#include <stdio.h>
#include <sys/time.h>

int main (int argc, char** argv) {
    struct timeval, tvalBefore, tvalAfter;

    gettimeofday (&tvalBefore, NULL);
    int i =0;
    while ( i < 1000) {
        i ++;
    }

    gettimeofday (&tvalAfter, NULL);

    printf("Time in microseconds: %0.3f microseconds\n",
            (float)(tvalAfter.tv_sec - tvalBefore.tv_sec) 
          )
    return 0;
}

铛编译器给了我以下错误:

The clang compiler gives me the following errors:

print_time.c:7:16: error: expected identifier or '('
        struct timeval, *tvalBefore, *tvalAfter;
                      ^
print_time.c:13:17: error: use of undeclared identifier 'tvalBefore'
        gettimeofday (&tvalBefore, NULL);
                       ^
print_time.c:19:17: error: use of undeclared identifier 'tvalAfter'
        gettimeofday (&tvalAfter, NULL);
                       ^
print_time.c:22:12: error: use of undeclared identifier 'tvalAfter'
                        (float)(tvalAfter.tv_sec - tvalBefore.tv_sec) 
                                ^
print_time.c:22:31: error: use of undeclared identifier 'tvalBefore'
                        (float)(tvalAfter.tv_sec - tvalBefore.tv_sec) 
                                                   ^
5 errors generated.

我想不出有什么不对我的code,任何想法?

I can't figure out what's wrong with my code, any idea?

推荐答案

您在你的code两个打字错误:

You have two typing errors in your code:

 struct timeval,

 struct timeval

的printf()括号后需要一个分号。

此外,根据不同的编译器,那么简单的一个周期可能只是被优化了,给你的0微秒无论你做什么的时候。

Also, depending on the compiler, so simple a cycle might just be optimized out, giving you a time of 0 microseconds whatever you do.

最后,时间的计算是错误的。你只考虑到占秒,无视微秒。你需要得到秒之差,一百万大量繁殖,然后添加之后 tv_usec 和前减去 tv_usec 。您可以通过铸造的整数秒为float一无所获。

Finally, the time calculation is wrong. You only take into accounts the seconds, ignoring the microseconds. You need to get the difference between seconds, multiply by one million, then add "after" tv_usec and subtract "before" tv_usec. You gain nothing by casting an integer number of seconds to a float.

我建议你检查出 timeval结构的手册页。

I'd suggest checking out the man page for struct timeval.

这是code:

#include <stdio.h>
#include <sys/time.h>

int main (int argc, char** argv) {
    struct timeval tvalBefore, tvalAfter;  // removed comma

    gettimeofday (&tvalBefore, NULL);
    int i =0;
    while ( i < 10000) {
        i ++;
    }

    gettimeofday (&tvalAfter, NULL);

    // Changed format to long int (%ld), changed time calculation

    printf("Time in microseconds: %ld microseconds\n",
            ((tvalAfter.tv_sec - tvalBefore.tv_sec)*1000000L
           +tvalAfter.tv_usec) - tvalBefore.tv_usec
          ); // Added semicolon
    return 0;
}

这篇关于如何使用timeval结构,以获得执行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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