sleep()???這是windows函數(shù),但也不是耗費cpu時間,線程的操作
成都創(chuàng)新互聯(lián)公司專注于梁平網(wǎng)站建設服務及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供梁平營銷型網(wǎng)站建設,梁平網(wǎng)站制作、梁平網(wǎng)頁設計、梁平網(wǎng)站官網(wǎng)定制、小程序開發(fā)服務,打造梁平網(wǎng)絡公司原創(chuàng)品牌,更為您提供梁平網(wǎng)站排名全網(wǎng)營銷落地服務。
times() 函數(shù) | 獲取進程時間函數(shù)
函數(shù)原型 :
引用
#include sys/times.h
clock_t times (struct tms * buf );
函數(shù)功能 :
獲取進程時間。
說明 :
times() 函數(shù)返回從過去一個任意的時間點所經(jīng)過的時鐘數(shù)。返回值可能會超出 clock_t (一般為 long 型) 的范圍(溢出)。如果發(fā)生錯誤,則返回 (clock_t ) -1 類型,然后設置相應的 errno 值。
系統(tǒng)每秒的時鐘可以通過 sysconf(_SC_CLK_TCK); 函數(shù)獲得。關于 sysconf() 函數(shù)的詳細信息見:
上面 _SC_CLK_TCK 的值為 2,因為它在 /usr/include/bits/confname.h 頭文件的一個枚舉類型里定義。
struct tms 結構體定義在 sys/times.h 頭文件里,具體定義如下:
引用
/* Structure describing CPU time used by a process and its children. */
struct tms
{
clock_t tms_utime ; /* User CPU time. 用戶程序 CPU 時間*/
clock_t tms_stime ; /* System CPU time. 系統(tǒng)調用所耗費的 CPU 時間 */
clock_t tms_cutime ; /* User CPU time of dead children. 已死掉子進程的 CPU 時間*/
clock_t tms_cstime ; /* System CPU time of dead children. 已死掉子進程所耗費的系統(tǒng)調用 CPU 時間*/
};
實例驗證 times() 函數(shù)
測試代碼-1 :
引用
#include stdio.h
#include unistd.h
#include stdlib.h
#include sys/times.h
int main ()
{
struct tms time_buf_head , time_buf_end ;
long tck = 0 ;
clock_t time_head , time_end ;
tck = sysconf (_SC_CLK_TCK ); /*獲取系統(tǒng)時鐘(1秒里有多少個)*/
time_head = times ( time_buf_head ); /*進程運行到此時的系統(tǒng)時鐘數(shù)(總的)*/
printf ("head_time is : %f \n " , time_head / (double )tck ); /*此時進程所處的時間點(單位為秒)*/
//system ("./time_test.exe");
system ("sleep 2" ); /*睡眠2秒*/
time_end = times ( time_buf_end ); /*進程到此時的系統(tǒng)時鐘數(shù)*/
printf ("end_time is : %f \n " , time_end / (double )tck ); /*此時進程所處的時間點(單位為秒)*/
printf ("user time is : %f \n " , ((time_buf_end . tms_utime - time_buf_head . tms_utime ) / double )tck )); /*打印出用戶進程到此所經(jīng)歷時間*/
printf ("systime time is : %f \n " , ((time_buf_end . tms_stime - time_buf_head . tms_stime ) / double )tck ));
printf ("child user time is : %f \n " , ((time_buf_end . tms_cutime - time_buf_head . tms_cutime ) / (double )tck ));
printf ("child sys time is : %f \n " , ((time_buf_end . tms_cstime - time_buf_head . tms_cstime ) / (double )tck ));
return (0 );
} ( (
運行輸出 :
引用
beyes@beyes-groad:~$ ./time.exe
head_time is : 17236892.770000
end_time is : 17236894.790000
user time is : 0.000000
systime time is : 0.000000
child user time is : 0.000000
child sys time is : 0.000000
上面藍色部分的時間間隔剛好是 2s 。從上面看到,下面的時間值都是 0。為了驗證問題,現(xiàn)在修改一下源程序:
引用
#include sys/types.h
#include sys/stat.h
#include stdio.h
#include unistd.h
#include stdlib.h
#include fcntl.h
#include sys/times.h
int main ()
{
struct tms time_buf_head , time_buf_end ;
long tck = 0 ;
clock_t time_head , time_end ;
int i ;
int j ;
tck = sysconf (_SC_CLK_TCK );
time_head = times ( time_buf_head );
printf ("head_time is : %f \n " , time_head / (double )tck );
/*延遲測試用*/
for (i = 0 ; i 20000 ; i ++ )
for (j = 0 ; j 20000 ; j ++ ) {
;
}
time_end = times ( time_buf_end );
printf ("end_time is : %f \n " , time_end / (double )tck );
printf ("user time is : %f \n " , ((time_buf_end . tms_utime - time_buf_head . tms_utime ) / double )tck )); /*用戶進程所耗費的時間*/
printf ("systime time is : %f \n " , ((time_buf_end . tms_stime - time_buf_head . tms_stime ) / (double )tck ));
printf ("child user time is : %f \n " , ((time_buf_end . tms_cutime - time_buf_head . tms_cutime ) / (double )tck ));
printf ("child sys time is : %f \n " , ((time_buf_end . tms_cstime - time_buf_head . tms_cstime ) / (double )tck ));
return (0 );
} (
再次運行輸出:
引用
beyes@beyes-groad:~$ ./time.exe
head_time is : 17184643.070000
end_time is : 17184644.280000
user time is : 1.200000
systime time is : 0.000000
child user time is : 0.000000
child sys time is : 0.000000
由于使用了大量的延遲,這時可以看到 user time 里耗費了 1.2s ,而 end_time 和 head_time 的時間間隔為 1.21s 約等于 1.2s 。
再來修改一下代碼:
引用
#include sys/types.h
#include sys/stat.h
#include stdio.h
#include unistd.h
#include stdlib.h
#include fcntl.h
#include sys/times.h
int main ()
{
struct tms time_buf_head , time_buf_end ;
long tck = 0 ;
clock_t time_head , time_end ;
int i ;
int j ;
tck = sysconf (_SC_CLK_TCK );
time_head = times ( time_buf_head );
printf ("head_time is : %f \n " , time_head / (double )tck );
for (i = 0 ; i 1000 ; i ++ )
for (j = 0 ; j 1000 ; j ++ ) {
open ("Cannon-1.txt" , O_RDONLY );
}
time_end = times ( time_buf_end );
printf ("end_time is : %f \n " , time_end / (double )tck );
printf ("user time is : %f \n " , ((time_buf_end . tms_utime - time_buf_head . tms_utime ) / double )tck ));
printf ("systime time is : %f \n " , ((time_buf_end . tms_stime - time_buf_head . tms_stime ) / (double )tck ));
printf ("child user time is : %f \n " , ((time_buf_end . tms_cutime - time_buf_head . tms_cutime ) / (double )tck ));
printf ("child sys time is : %f \n " , ((time_buf_end . tms_cstime - time_buf_head . tms_cstime ) / (double )tck ));
return (0 );
}
(
運行輸出:
引用
beyes@beyes-groad:~$ ./time.exe
head_time is : 17189923.210000
end_time is : 17189923.650000
user time is : 0.160000
systime time is : 0.280000
child user time is : 0.000000
child sys time is : 0.000000
在上面的輸出中可以看到,systime time 這時不再為 0,這是因為程序中使用了 open() 這個系統(tǒng)調用的結果,它在兩個 for 循環(huán)里一共被調用了 1000*1000次,總耗時為0.28s ,而執(zhí)行這兩個 for 的時間為 0.16s ,兩個時間加起來的時間間隔正好為 end_time - head_time = 0.44s 。
下面測試子進程的時間,也就是 child user time 和 child sys time 兩個。這里,需要另外一個程序,它的主要作用就是和上面一樣的調用 open() 打開一個在本目錄下的一文本文件,代碼如下:
引用
#include stdio.h
#include sys/types.h
#include sys/stat.h
#include fcntl.h
int main ()
{
int i ;
int j ;
for (i = 0 ; i 1000 ; i ++ )
for (j = 0 ; j 1000 ; j ++ ) {
open ("Cannon-1.txt" , O_RDONLY );
}
return (0 );
}
上面的程序命名為 time_test.exe ,它會在主程序里被 system() 函數(shù)調用到,修改主程序如下:
引用
#include sys/types.h
#include sys/stat.h
#include stdio.h
#include unistd.h
#include stdlib.h
#include fcntl.h
#include sys/times.h
int main ()
{
struct tms time_buf_head , time_buf_end ;
long tck = 0 ;
clock_t time_head , time_end ;
int i ;
int j ;
tck = sysconf (_SC_CLK_TCK );
time_head = times ( time_buf_head );
printf ("head_time is : %f \n " , time_head / (double )tck );
system ("./time_test.exe" );
time_end = times ( time_buf_end );
printf ("end_time is : %f \n " , time_end / (double )tck );
printf ("user time is : %f \n " , ((time_buf_end . tms_utime - time_buf_head . tms_utime ) / double )tck ));
printf ("systime time is : %f \n " , ((time_buf_end . tms_stime - time_buf_head . tms_stime ) / (double )tck ));
printf ("child user time is : %f \n " , ((time_buf_end . tms_cutime - time_buf_head . tms_cutime ) / (double )tck ));
printf ("child sys time is : %f \n " , ((time_buf_end . tms_cstime - time_buf_head . tms_cstime ) / (double )tck ));
return (0 );
} (
運行輸出:
引用
beyes@beyes-groad:~$ ./time.exe
head_time is : 17190766.590000
end_time is : 17190767.060000
user time is : 0.000000
systime time is : 0.000000
child user time is : 0.140000
child sys time is : 0.300000
由上可見,child user time 和 child sys time 兩者的時間也是為 0.44s,這和上面是一樣的,這是因為程序的內容相同。
這個一般不能做到。
一般的C語言編譯器會限制程序的資源使用量(如CPU不超過40%),超過設定時,編譯出來的程序會提示“未響應”而停止工作,不會造成電腦滿負荷工作的情況。
實現(xiàn)滿負荷工作可以用一些拷機軟件,比如Super?PI,來測試處理器的性能。
但對于時間的記錄,可以運用time.h的函數(shù)來實現(xiàn),用法如下:
time_t?start,end;??
start?=time(NULL);//or?time(start);??
//計時中
end?=time(NULL);??
printf("time=%d\n",difftime(end,start));
這里的輸出單位為秒。如要精確到毫秒的計時,可以調用clock():
clock_t?start,end;??
start?=?clock();??
//計時中?
end?=?clock();??
printf("time=%f\n",(double)end-start)/CLK_TCK);
這里end和start得到的是計算機時鐘的tick數(shù),換算成毫秒需要除以常數(shù)CLK_TCK,換算成秒除以常數(shù)CLK_TCKCLOCKS_PER_SEC。
呵呵 這個沒法說 最好還是用 bioskey吧 用延時 不交還系統(tǒng)控制權自然會100%占用cpu
不用api 在winnt以上機器 我還真沒有別的辦法 呵呵
c-free是gcc的一個win下的ide吧 試試 用api
GetKeyState
WaitForSingleObject
詳情查閱msdn
分享文章:耗費cpu的c語言函數(shù) 耗費cpu的c語言函數(shù)怎么用
當前URL:http://www.muchs.cn/article26/docssjg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供外貿建站、Google、域名注冊、企業(yè)網(wǎng)站制作、虛擬主機、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)