linux函数计时,Linux 中的计时——gettimeofday函数
1.使用C語言進行計時
在用戶空間中可以使用C語言函數gettimeofday 得到時間,它的調用格式是:
#include
int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv , const struct timezone *tz);
結構timeval的定義為:
strut timeval {long tv_sec; /* 秒數 */long tv_usec; /* 微秒數 */};
可以看出,使用這種方式計時,精度可達微秒,也就是10-6秒。進行計時的時候,我們需要前后調用兩次gettimeofday,然后計算中間的差值:
gettimeofday( &start, NULL );
foo();
gettimeofday( &end, NULL );
得到微秒: timeuse = 1000000 * ( end.tv_sec - start.tv_sec ) + end.tv_usec - start.tv_usec;
得到秒: timeuse /= 1000000;
2.在python中,得到微秒
begin = datetime.datetime.today() end = datetime.datetime.today() ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? print end.microsecond ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? print begin.microsecond ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? td = end - begin ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? print 'create introduce: %f seconds'%((td.microseconds + 1.0 * (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6)
總結
以上是生活随笔為你收集整理的linux函数计时,Linux 中的计时——gettimeofday函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 简单的电脑制图软件有哪些(电脑做图软件有
- 下一篇: 夸奖一个人很有才华的句子248个