如何在C ++中使用clock()函数
在这里,我们将看到如何clock()在C++中使用。它clock()存在于time.h或ctime头文件中。在这里,我们将找到使用此clock()方法的过程所花费的时间
要获得经过的时间,我们可以clock()在taks的开始和结尾使用来获取时间,然后减去这些值以获得差值。之后,我们将差异除以CLOCK_PER_SEC(每秒的时钟滴答数)以获得处理器时间。
示例
#include <iostream>
#include <ctime>
using namespace std;
void take_enter() {
cout << "Press enter to stop the counter" <<endl;
while(1) {
if (getchar())
break;
}
}
main() {
//花费的时间
clock_t t;
t = clock();
cout << "Timer starts\n";
take_enter();
cout << "Timer ends \n";
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time
cout << "该计划采取 "<< time_taken <<" seconds to execute";
}输出结果
Timer starts Press enter to stop the counter Timer ends 该计划采取 3.546 seconds to execute
热门推荐
10 送女友鲜花祝福语简短
11 祝女儿结婚简短祝福语
12 除夕祝福语大全简短年轻
13 长辈请客祝福语简短精辟
14 牛年的吉祥祝福语简短
15 圣诞祝福语大全简短星座
16 生日祝福语 男性简短独特
17 爸爸寿宴仪式祝福语简短
18 节日祝福语简短中秋暧昧