linux timeval gettimeofday printf 错误
linux timeval gettimeofday printf error
函数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));
}
gettimeofday
需要一个指向 struct timeval
的有效指针,它可以在其中保存信息,您可以用 NULL
指针调用它。
你应该改变
struct timeval *timeBefore;
struct timeval *timeAfter;
至
struct timeval timeBefore;
struct timeval timeAfter;
以及对 gettimeofday(&timeBefore, NULL)
和 gettimeofday(&timeAfter, NULL)
的调用。您检查了此函数的 return 值并打印了一些内容,但您的程序继续成功运行。
还有
printf("Time in microseconds: %ld microseconds\n", (timeAfter->tv_sec - timeBefore->tv_sec));
至
printf("Time in seconds: %ld microseconds\n", (timeAfter.tv_sec - timeBefore.tv_sec));
.
你只是在计算秒,而不是微秒。
另一种可能性是 malloc
指针的内存,但这并不是真正必要的。
如另一个答案中所述,您错误地将 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;
函数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));
}
gettimeofday
需要一个指向 struct timeval
的有效指针,它可以在其中保存信息,您可以用 NULL
指针调用它。
你应该改变
struct timeval *timeBefore;
struct timeval *timeAfter;
至
struct timeval timeBefore;
struct timeval timeAfter;
以及对 gettimeofday(&timeBefore, NULL)
和 gettimeofday(&timeAfter, NULL)
的调用。您检查了此函数的 return 值并打印了一些内容,但您的程序继续成功运行。
还有
printf("Time in microseconds: %ld microseconds\n", (timeAfter->tv_sec - timeBefore->tv_sec));
至
printf("Time in seconds: %ld microseconds\n", (timeAfter.tv_sec - timeBefore.tv_sec));
.
你只是在计算秒,而不是微秒。
另一种可能性是 malloc
指针的内存,但这并不是真正必要的。
如另一个答案中所述,您错误地将 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;