【问题标题】:linux timeval gettimeofday printf errorlinux timeval gettimeofday printf错误
【发布时间】:2015-10-24 14:39:29
【问题描述】:

函数displayTimeDifference不能正常工作;问题是 printf 语句失败。在使用 timeval 时使用谷歌搜索 printf 语句的格式是正确的。不知道为什么我不能打印出 timeval 的值。我没有从 gettimeofday() 收到任何系统错误。

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

struct timeval *timeBefore;
struct timeval *timeAfter;
char * Buffer;

double malloctest(const int, const int, const int);
double calloctest(const int, const int, const int);
double allocatest(const int, const int, const int);
void   displayTimeDifference();

int main(int argc, char **argv)
{
    malloctest(3072, 10, 10);
    return 0;
}

double malloctest(const int objectsize, const int numobjects, const int numtests)
{
    int i;
    int retVal;
    for (i = 1; i < numtests; i++) {
        if ((retVal = gettimeofday(timeBefore, NULL)) != 0) {
            printf("ERROR: gettimeofday failed with code: %d\n", retVal);
        }

        Buffer = (char*)malloc(objectsize * sizeof(char));

        if ((retVal = gettimeofday(timeAfter, NULL)) != 0) {
            printf("ERROR: gettimeofday failed with code: %d\n", retVal);
        }

        displayTimeDifference();
    }

    return 0.0;
}



void displayTimeDifference()
{
    printf("Time in microseconds: %ld microseconds\n", (timeAfter->tv_sec - timeBefore->tv_sec));
}

【问题讨论】:

  • 哪里出了问题?你调试了吗? Buffer 是怎么回事? 确切的错误信息是什么?
  • 请注意,尽管您声称打印微秒,但您尝试以秒为单位计算两次之间的差异。如果您在 123.987654 之前和 125.012987 之后获得时间,您必须做更多的工作。

标签: c linux time struct system-calls


【解决方案1】:

gettimeofday 需要一个指向struct timeval 的有效指针,它可以保存信息,您可以使用NULL 指针调用它。

你应该改变

struct timeval *timeBefore;
struct timeval *timeAfter;

struct timeval timeBefore;
struct timeval timeAfter;

以及对gettimeofday(&amp;timeBefore, NULL)gettimeofday(&amp;timeAfter, NULL) 的调用。你检查了这个函数的返回值并打印了一些东西,但是你的程序继续成功。

还有
printf("Time in microseconds: %ld microseconds\n", (timeAfter-&gt;tv_sec - timeBefore-&gt;tv_sec));

printf("Time in seconds: %ld microseconds\n", (timeAfter.tv_sec - timeBefore.tv_sec));.
您只计算秒数,而不是微秒数。

另一种可能性是malloc 指针的内存,但这不是必需的。

【讨论】:

    【解决方案2】:

    正如在另一个答案中已经说过的那样,您错误地将 struct timeval 声明为指针。 我分享我的计时宏:

    #define START_TIMER(begin)  gettimeofday(&begin, NULL) // ;
    
    #define END_TIMER(end)      gettimeofday(&end,   NULL) // ;
    
    //get the total number of sec:
    #define ELAPSED_TIME(elapsed, begin, end) \
        elapsed = (end.tv_sec - begin.tv_sec) \
        + ((end.tv_usec - begin.tv_usec)/1000000.0) // ;
    

    你必须在哪里定义变量:

    struct timeval begin, end;
    double elapsed;
    

    【讨论】:

    • '奇怪的是,您在原本可能有用的表达式中强制进行赋值。因为您使用double,所以如果您将时间(以秒为单位)与以微秒为单位的时间分开(如在 125.012987 - 123.987345 中),则可以避免因差异而遇到的问题。如果将秒和微秒分开,则必须处理负微秒和补偿加减法。
    • 另外,去掉#define 行之后的分号。他们错了。如果用户写:if (onevar == twovar) ELAPSED_TIME(t_delta, t1, t2); else ELAPSED_TIME(t_delta, t3, t4);,代码将无法编译。与开始计时器和结束计时器宏类似。
    猜你喜欢
    • 2012-04-27
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    • 2019-12-02
    • 1970-01-01
    相关资源
    最近更新 更多