(1)使用命令time:
[root@localhost-120 xgf]# time ./standard
counter = 1000000
real0m0.006s
user0m0.000s
sys 0m0.000s
time命令对秒(s)级别的很精确,而对毫秒级的误差比价大。我们可以通过sleep/usleep函数来进行测试。sleep(0.1)或者usleep(100)都是表示休眠100ms,而测试结果都是:
real0m0.002s
user0m0.000s
sys 0m0.000s
(2)通过difftime函数:
double difftime(time_t time1, time_t time0)计算time1和time0之间的秒数。测试程序如下:
#include <stdio.h>
#include <time.h>
#define MAX 1000
int do_work()
{
int counter = 0 /* the counter */
int i, j /* the loop variable */
/* accumulate the counter */
for(i = 0i <MAXi++)
for(j = 0j <MAXj++)
counter++
/* return the counter's value */
return counter
}
int main()
{
time_t start, end
int val
start = time(NULL)
do_work()
end = time(NULL)
printf("val = %f/n", difftime(end, start))
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)