资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

耗费cpu的c语言函数 耗费cpu的c语言函数怎么用

在C语言中有一种语句不实现任何功能,但是会耗费CPU时间,这种语句叫做什么?

sleep()???这是windows函数,但也不是耗费cpu时间,线程的操作

成都创新互联公司专注于梁平网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供梁平营销型网站建设,梁平网站制作、梁平网页设计、梁平网站官网定制、小程序开发服务,打造梁平网络公司原创品牌,更为您提供梁平网站排名全网营销落地服务。

c语言函数times()问题

times() 函数 | 获取进程时间函数

函数原型 :

引用

#include sys/times.h

clock_t times (struct tms * buf );

函数功能 :

获取进程时间。

说明 :

times() 函数返回从过去一个任意的时间点所经过的时钟数。返回值可能会超出 clock_t (一般为 long 型) 的范围(溢出)。如果发生错误,则返回 (clock_t ) -1 类型,然后设置相应的 errno 值。

系统每秒的时钟可以通过 sysconf(_SC_CLK_TCK); 函数获得。关于 sysconf() 函数的详细信息见:

上面 _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. 系统调用所耗费的 CPU 时间 */

clock_t tms_cutime ; /* User CPU time of dead children. 已死掉子进程的 CPU 时间*/

clock_t tms_cstime ; /* System CPU time of dead children. 已死掉子进程所耗费的系统调用 CPU 时间*/

};

实例验证 times() 函数

测试代码-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 ); /*获取系统时钟(1秒里有多少个)*/

time_head = times ( time_buf_head ); /*进程运行到此时的系统时钟数(总的)*/

printf ("head_time is : %f \n " , time_head / (double )tck ); /*此时进程所处的时间点(单位为秒)*/

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

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

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 : 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。为了验证问题,现在修改一下源程序:

引用

#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() 这个系统调用的结果,它在两个 for 循环里一共被调用了 1000*1000次,总耗时为0.28s ,而执行这两个 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() 函数调用到,修改主程序如下:

引用

#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语言大师请给我写一个c程序用来测试我机器CPU满负荷工作

这个一般不能做到。

一般的C语言编译器会限制程序的资源使用量(如CPU不超过40%),超过设定时,编译出来的程序会提示“未响应”而停止工作,不会造成电脑满负荷工作的情况。

实现满负荷工作可以用一些拷机软件,比如Super PI,来测试处理器的性能。

但对于时间的记录,可以运用time.h的函数来实现,用法如下:

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数,换算成毫秒需要除以常数CLK_TCK,换算成秒除以常数CLK_TCKCLOCKS_PER_SEC。

C语言中使用bioskey函数cpu占用100%

呵呵 这个没法说 最好还是用 bioskey吧 用延时 不交还系统控制权自然会100%占用cpu

不用api 在winnt以上机器 我还真没有别的办法 呵呵

c-free是gcc的一个win下的ide吧 试试 用api

GetKeyState

WaitForSingleObject

详情查阅msdn


网站题目:耗费cpu的c语言函数 耗费cpu的c语言函数怎么用
当前网址:http://cdkjz.cn/article/docssjg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220