最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
耗費(fèi)cpu的c語(yǔ)言函數(shù) 耗費(fèi)cpu的c語(yǔ)言函數(shù)怎么用

在C語(yǔ)言中有一種語(yǔ)句不實(shí)現(xiàn)任何功能,但是會(huì)耗費(fèi)CPU時(shí)間,這種語(yǔ)句叫做什么?

sleep()???這是windows函數(shù),但也不是耗費(fèi)cpu時(shí)間,線程的操作

成都創(chuàng)新互聯(lián)公司專注于梁平網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供梁平營(yíng)銷型網(wǎng)站建設(shè),梁平網(wǎng)站制作、梁平網(wǎng)頁(yè)設(shè)計(jì)、梁平網(wǎng)站官網(wǎng)定制、小程序開發(fā)服務(wù),打造梁平網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供梁平網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。

c語(yǔ)言函數(shù)times()問(wèn)題

times() 函數(shù) | 獲取進(jìn)程時(shí)間函數(shù)

函數(shù)原型 :

引用

#include sys/times.h

clock_t times (struct tms * buf );

函數(shù)功能 :

獲取進(jìn)程時(shí)間。

說(shuō)明 :

times() 函數(shù)返回從過(guò)去一個(gè)任意的時(shí)間點(diǎn)所經(jīng)過(guò)的時(shí)鐘數(shù)。返回值可能會(huì)超出 clock_t (一般為 long 型) 的范圍(溢出)。如果發(fā)生錯(cuò)誤,則返回 (clock_t ) -1 類型,然后設(shè)置相應(yīng)的 errno 值。

系統(tǒng)每秒的時(shí)鐘可以通過(guò) sysconf(_SC_CLK_TCK); 函數(shù)獲得。關(guān)于 sysconf() 函數(shù)的詳細(xì)信息見(jiàn):

上面 _SC_CLK_TCK 的值為 2,因?yàn)樗?/usr/include/bits/confname.h 頭文件的一個(gè)枚舉類型里定義。

struct tms 結(jié)構(gòu)體定義在 sys/times.h 頭文件里,具體定義如下:

引用

/* Structure describing CPU time used by a process and its children. */

struct tms

{

clock_t tms_utime ; /* User CPU time. 用戶程序 CPU 時(shí)間*/

clock_t tms_stime ; /* System CPU time. 系統(tǒng)調(diào)用所耗費(fèi)的 CPU 時(shí)間 */

clock_t tms_cutime ; /* User CPU time of dead children. 已死掉子進(jìn)程的 CPU 時(shí)間*/

clock_t tms_cstime ; /* System CPU time of dead children. 已死掉子進(jìn)程所耗費(fèi)的系統(tǒng)調(diào)用 CPU 時(shí)間*/

};

實(shí)例驗(yàn)證 times() 函數(shù)

測(cè)試代碼-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)時(shí)鐘(1秒里有多少個(gè))*/

time_head = times ( time_buf_head ); /*進(jìn)程運(yùn)行到此時(shí)的系統(tǒng)時(shí)鐘數(shù)(總的)*/

printf ("head_time is : %f \n " , time_head / (double )tck ); /*此時(shí)進(jìn)程所處的時(shí)間點(diǎn)(單位為秒)*/

//system ("./time_test.exe");

system ("sleep 2" ); /*睡眠2秒*/

time_end = times ( time_buf_end ); /*進(jìn)程到此時(shí)的系統(tǒng)時(shí)鐘數(shù)*/

printf ("end_time is : %f \n " , time_end / (double )tck ); /*此時(shí)進(jìn)程所處的時(shí)間點(diǎn)(單位為秒)*/

printf ("user time is : %f \n " , ((time_buf_end . tms_utime - time_buf_head . tms_utime ) / double )tck )); /*打印出用戶進(jìn)程到此所經(jīng)歷時(shí)間*/

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 );

} ( (

運(yùn)行輸出 :

引用

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

上面藍(lán)色部分的時(shí)間間隔剛好是 2s 。從上面看到,下面的時(shí)間值都是 0。為了驗(yàn)證問(wèn)題,現(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 );

/*延遲測(cè)試用*/

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 )); /*用戶進(jìn)程所耗費(fèi)的時(shí)間*/

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 );

} (

再次運(yùn)行輸出:

引用

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

由于使用了大量的延遲,這時(shí)可以看到 user time 里耗費(fèi)了 1.2s ,而 end_time 和 head_time 的時(shí)間間隔為 1.21s 約等于 1.2s 。

再來(lái)修改一下代碼:

引用

#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 );

}

(

運(yùn)行輸出:

引用

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 這時(shí)不再為 0,這是因?yàn)槌绦蛑惺褂昧?open() 這個(gè)系統(tǒng)調(diào)用的結(jié)果,它在兩個(gè) for 循環(huán)里一共被調(diào)用了 1000*1000次,總耗時(shí)為0.28s ,而執(zhí)行這兩個(gè) for 的時(shí)間為 0.16s ,兩個(gè)時(shí)間加起來(lái)的時(shí)間間隔正好為 end_time - head_time = 0.44s 。

下面測(cè)試子進(jìn)程的時(shí)間,也就是 child user time 和 child sys time 兩個(gè)。這里,需要另外一個(gè)程序,它的主要作用就是和上面一樣的調(diào)用 open() 打開一個(gè)在本目錄下的一文本文件,代碼如下:

引用

#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 ,它會(huì)在主程序里被 system() 函數(shù)調(diào)用到,修改主程序如下:

引用

#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 );

} (

運(yùn)行輸出:

引用

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

由上可見(jiàn),child user time 和 child sys time 兩者的時(shí)間也是為 0.44s,這和上面是一樣的,這是因?yàn)槌绦虻膬?nèi)容相同。

C語(yǔ)言大師請(qǐng)給我寫一個(gè)c程序用來(lái)測(cè)試我機(jī)器CPU滿負(fù)荷工作

這個(gè)一般不能做到。

一般的C語(yǔ)言編譯器會(huì)限制程序的資源使用量(如CPU不超過(guò)40%),超過(guò)設(shè)定時(shí),編譯出來(lái)的程序會(huì)提示“未響應(yīng)”而停止工作,不會(huì)造成電腦滿負(fù)荷工作的情況。

實(shí)現(xiàn)滿負(fù)荷工作可以用一些拷機(jī)軟件,比如Super?PI,來(lái)測(cè)試處理器的性能。

但對(duì)于時(shí)間的記錄,可以運(yùn)用time.h的函數(shù)來(lái)實(shí)現(xiàn),用法如下:

time_t?start,end;??

start?=time(NULL);//or?time(start);??

//計(jì)時(shí)中

end?=time(NULL);??

printf("time=%d\n",difftime(end,start));

這里的輸出單位為秒。如要精確到毫秒的計(jì)時(shí),可以調(diào)用clock():

clock_t?start,end;??

start?=?clock();??

//計(jì)時(shí)中?

end?=?clock();??

printf("time=%f\n",(double)end-start)/CLK_TCK);

這里end和start得到的是計(jì)算機(jī)時(shí)鐘的tick數(shù),換算成毫秒需要除以常數(shù)CLK_TCK,換算成秒除以常數(shù)CLK_TCKCLOCKS_PER_SEC。

C語(yǔ)言中使用bioskey函數(shù)cpu占用100%

呵呵 這個(gè)沒(méi)法說(shuō) 最好還是用 bioskey吧 用延時(shí) 不交還系統(tǒng)控制權(quán)自然會(huì)100%占用cpu

不用api 在winnt以上機(jī)器 我還真沒(méi)有別的辦法 呵呵

c-free是gcc的一個(gè)win下的ide吧 試試 用api

GetKeyState

WaitForSingleObject

詳情查閱msdn


名稱欄目:耗費(fèi)cpu的c語(yǔ)言函數(shù) 耗費(fèi)cpu的c語(yǔ)言函數(shù)怎么用
URL鏈接:http://fisionsoft.com.cn/article/docssjg.html