【发布时间】: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