linux c 时间问题
错误:‘starttime’的存储大小未知
‘stoptime’的存储大小未知
#include <stdio.h>
#include <time.h>
#include <math.h>
void func()
{
unsigned int i,j;
double y;
for( i=0; i<1000 ; ++i )
{
for( j=0; j<1000 ; ++j )
{
y=sin((double)i);
}
}
}
int main(int argc, char *argv[])
{
struct timeval starttime,stoptime;
double usetime;
gettimeofday(starttime,NULL);
func();
gettimeofday(stoptime,NULL);
usetime=(starttime.tv_sec - stoptime.tv_sec)*
1000000+
starttime.tv_usec - stoptime.tv_usec;
usetime /= 1000000;
printf("the func run use time %lf\n",usetime);
return 0;
}
[解决办法]
- C/C++ code
#include <stdio.h>#include <time.h>#include <math.h>#include <sys/time.h>void func(){ unsigned int i,j; double y; for( i=0; i<1000 ; ++i ) { for( j=0; j<1000 ; ++j ) { y=sin((double)i); } }}int main(int argc, char *argv[]){ struct timeval starttime; struct timeval stoptime; double usetime; gettimeofday(&starttime,NULL); func(); gettimeofday(&stoptime,NULL); usetime=(starttime.tv_sec - stoptime.tv_sec)* 1000000+ starttime.tv_usec - stoptime.tv_usec; usetime /= 1000000; printf("the func run use time %lf\n",usetime); return 0;}
[解决办法]
gettimeofday(starttime,NULL);
调用函数后,传入的参数是不会改变的,尽管在函数里操作了;而如果是传入参数的地址,则调用函数操作,可以改变参数值。而且gettimeofday(&starttime, NULL)是这样用的。