C/C()中的时钟函数++

clock()函数是在 时间 头文件。函数的作用是:返回程序消耗的处理器时间。clock()时间取决于操作系统如何为进程分配资源,这就是clock()时间可能比实际时钟慢或快的原因。

null

语法:

clock_t clock( void );

参数: 此函数不接受任何参数。

返回值: 此函数返回程序消耗的处理器时间近似值,失败时函数返回-1。

下面的程序演示了clock()函数的实现:

// C++ program to demonstrate
// example of clock() function.
#include<bits/stdc++.h>
using namespace std;
int main ()
{
float a;
clock_t time_req;
// Without using pow function
time_req = clock ();
for ( int i=0; i<200000; i++)
{
a = log (i*i*i*i);
}
time_req = clock ()- time_req;
cout << "Processor time taken for multiplication: "
<< ( float )time_req/CLOCKS_PER_SEC << " seconds" << endl;
// Using pow function
time_req = clock ();
for ( int i=0; i<200000; i++)
{
a = log ( pow (i, 4));
}
time_req = clock () - time_req;
cout << "Processor time taken in pow function: "
<< ( float )time_req/CLOCKS_PER_SEC << " seconds" << endl;
return 0;
}


输出:

Processor time taken for multiplication: 0.006485 seconds
Processor time taken in pow function: 0.022251 seconds

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享