为什么CLOCKS_PER_SEC不是每秒钟的实际时钟数? [英] Why is CLOCKS_PER_SEC not the actual number of clocks per second?
问题描述
#include< p>我已经写了这个简短的C ++程序来估计每秒的实际时钟节拍数。 iostream>
#include< time.h>
using namespace std;
int main(){
for(int i = 0; i <10; i ++){
int first_clock = clock ;
int first_time = time(NULL);
while(time(NULL)< = first_time){}
int second_time = time(NULL);
int second_clock = clock();
cout<< 实际时钟每秒=< (second_clock - first_clock)/(second_time-first_time)< \\\
;
cout<< CLOCKS_PER_SEC =< CLOCKS_PER_SEC<< \\\
;
}
return 0;
}
当我运行程序时,
每秒实际时钟数= 199139
CLOCKS_PER_SEC = 1000000
每秒实际时钟数= 638164
CLOCKS_PER_SEC = 1000000
每秒实际时钟数= 610735
CLOCKS_PER_SEC = 1000000
每秒实际时钟数= 614835
CLOCKS_PER_SEC = 1000000
每秒实际时钟数= 642327
CLOCKS_PER_SEC = 1000000
每秒实际时钟数= 562068
CLOCKS_PER_SEC = 1000000
每秒实际时钟数= 605767
CLOCKS_PER_SEC = 1000000
每秒实际时钟数= 619543
CLOCKS_PER_SEC = 1000000
每秒实际时钟数= 650243
CLOCKS_PER_SEC = 1000000
每秒实际时钟数= 639128
CLOCKS_PER_SEC = 1000000
为什么CLOCKS_PER_SEC每秒钟的实际时钟滴答数不匹配?他们甚至不相等。这是怎么回事?
clock
返回花费的时间 每秒总共有1,000,000个时钟滴答。 * 。您的程序似乎占用了其中的60%。
其他40%的用途。
sup> * 好的,每秒都有虚拟 1,000,000个时钟滴答。实际数字是规范化的,因此您的程序可以感知到1,000,000个tick。
I have just written this short C++ program to approximate the actual number of clock ticks per second.
#include <iostream>
#include <time.h>
using namespace std;
int main () {
for(int i = 0; i < 10 ; i++) {
int first_clock = clock();
int first_time = time(NULL);
while(time(NULL) <= first_time) {}
int second_time = time(NULL);
int second_clock = clock();
cout << "Actual clocks per second = " << (second_clock - first_clock)/(second_time - first_time) << "\n";
cout << "CLOCKS_PER_SEC = " << CLOCKS_PER_SEC << "\n";
}
return 0;
}
When I run the program, I get output that looks like this.
Actual clocks per second = 199139
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 638164
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 610735
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 614835
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 642327
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 562068
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 605767
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 619543
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 650243
CLOCKS_PER_SEC = 1000000
Actual clocks per second = 639128
CLOCKS_PER_SEC = 1000000
Why doesn't the actual number of clock ticks per second match up with CLOCKS_PER_SEC? They're not even approximately equal. What's going on here?
clock
returns the amount of time spent in your program. There are 1,000,000 clock ticks per second total*. It appears that your program consumed 60% of them.
Something else used the other 40%.
*Okay, there are virtually 1,000,000 clock ticks per second. The actual number is normalized so your program perceives 1,000,000 ticks.
这篇关于为什么CLOCKS_PER_SEC不是每秒钟的实际时钟数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!