CLOCKS

编程入门 行业动态 更新时间:2024-10-28 11:21:17
CLOCKS_PER_SEC不匹配std :: clock()的结果(CLOCKS_PER_SEC Not Matching Results From std::clock())

我正在使用以下短程序来测试std::clock() :

#include <ctime> #include <iostream> int main() { std::clock_t Begin = std::clock(); int Dummy; std::cin >> Dummy; std::clock_t End = std::clock(); std::cout << "CLOCKS_PER_SEC: " << CLOCKS_PER_SEC << "\n"; std::cout << "Begin: " << Begin << "\n"; std::cout << "End: " << End << "\n"; std::cout << "Difference: " << (End - Begin) << std::endl; }

但是,在等待几秒钟输入“虚拟”值后,我得到以下输出:

CLOCKS_PER_SEC: 1000000 Begin: 13504 End: 13604 Difference: 100

这显然没有多大意义。 无论等待多久,差异总是在100左右。

我错过了什么? 是否有一些我忘记包含的标题?

我使用Xcode和GCC 4.2。

I'm using the following short program to test std::clock():

#include <ctime> #include <iostream> int main() { std::clock_t Begin = std::clock(); int Dummy; std::cin >> Dummy; std::clock_t End = std::clock(); std::cout << "CLOCKS_PER_SEC: " << CLOCKS_PER_SEC << "\n"; std::cout << "Begin: " << Begin << "\n"; std::cout << "End: " << End << "\n"; std::cout << "Difference: " << (End - Begin) << std::endl; }

However, after waiting several seconds to input the "dummy" value, I get the following output:

CLOCKS_PER_SEC: 1000000 Begin: 13504 End: 13604 Difference: 100

This obviously doesn't make much sense. No matter how long I wait, the difference is always somewhere around 100.

What am I missing? Is there some header I forgot to include?

I'm using Xcode with GCC 4.2.

最满意答案

clock()计算CPU时间,因此如果它坐在等待输入的位置,它不会增加任何时间。

clock() counts CPU time, so it's not adding any time if it's sitting around waiting for input.

更多推荐

本文发布于:2023-08-03 12:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390255.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:CLOCKS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!