linux:统计耗时

linux:统计耗时,第1张

linux下的方法:

(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

}

linuxsleep函数不准解决办法如下:

如下面的一段程序:

应用程序:

#include <syswait.h>

usleep(n) //n微秒

Sleep(n)//n毫秒

sleep(n)//n秒

驱动程序:

#include <linux/delay.h>

mdelay(n) //milliseconds 其实现

#ifdef notdef

#define mdelay(n) (\

{unsigned long msec=(n)while (msec--) udelay(1000)})

#else,linuxsleep函数不准就可以调整为正确的了。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/7339608.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-04
下一篇 2023-04-04

发表评论

登录后才能评论

评论列表(0条)

保存