CLOCKS_PER_SEC Not Matching Results From std::clock()

a 夏天 提交于 2019-12-10 17:27:49

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/6114392/clocks-per-sec-not-matching-results-from-stdclock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!