日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

linux函数计时,Linux 中的计时——gettimeofday函数

發布時間:2023/12/2 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。