CLOCKS_PER_SEC不匹配结果从std :: clock() [英] CLOCKS_PER_SEC Not Matching Results From std::clock()

查看:262
本文介绍了CLOCKS_PER_SEC不匹配结果从std :: clock()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
}

但是,等待几秒钟输入dummy以下输出:

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

这显然没什么意义。无论我等待多久,差异总是在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?

我使用Xcode与GCC 4.2。

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.

这篇关于CLOCKS_PER_SEC不匹配结果从std :: clock()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆