gettimeofday
gettimeofday
reference
manps.还有介绍settimeofday()
利用gettimeofday计算程序运行时间--精确到毫秒ps.有代码
futher reading
32位与64位下各类型长度对比ps.主要在于long和指针,还有64位系统下编程需要注意的几个问题。
The functions gettimeofday and settimeofday can get and set the time as
well as a timezone.
#include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz);
"tv"
The tv argument is a timeval struct, as specified in
struct timeval {
time_t tv_sec; /* seconds秒 */
suseconds_t tv_usec; /* microseconds毫秒 */
};
- gives the number of seconds and microseconds since the
Epoch:1970-01-01 00:00:00 +0000 (UTC). Traditionally, the fields of - struct timeval were of type long.
return value
gettimeofday() and settimeofday() return 0 for success, or -1 for
failure (in which case errno is set appropriately).
examples
1.ms
#include <sys/time.h>
#include <<font color="#ff0000">sys/time.h>
long getCurrentTime()
{
struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
int main()
{
long start = getCurrentTime();
printf("c/c++ program:%ld\n",getCurrentTime());
//设置延迟
for (int i = 0; i<100000; ++i)
{ ; }
printf("The program has run %ld ms", getCurrentTime()-start);
return 0;
}