[問題] 關於gettimeofday

看板C_and_CPP (C/C++)作者 (Light)時間13年前 (2012/10/21 17:55), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串1/1
以下是取程式執行時間的程式碼 #include <stdio.h> #include<sys/time.h> #include<unistd.h> #include <stdlib.h> int main() { struct timeval stime;//存放開始執行時間 struct timeval etime;//存放程式結束時間 gettimeofday(&stime,NULL);//抓取開始執行時間 //以下執行程式 .... //以上執行程式 gettimeofday(&etime,NULL);//抓取程式執行結束時間 long spentus= (etime.tv_sec*1000000+etime.tv_usec)-(stime.tv_sec*1000000+stime.tv_usec); int s=spentus/1000000; int us=spentus%1000000;//以上做時間間隔計算 printf("spent time:%d s %d us\n",s,us); return 0; } 爬文說tv.sec是取凌晨開始算起的秒數 可是我看不太懂tv.usec部分 他是怎麼取出值的呢? 為什麼不能直接取tv.usec直接去換算就好呢? 這樣取出來只有小數部分。 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.160.131.76

10/22 23:06, , 1F
tv_usec 本來就只有存小數部份(microsecond)
10/22 23:06, 1F
文章代碼(AID): #1GWyQ617 (C_and_CPP)
文章代碼(AID): #1GWyQ617 (C_and_CPP)