c – CLOCKS_PER_SEC与std :: clock()不匹配的结果

前端之家收集整理的这篇文章主要介绍了c – CLOCKS_PER_SEC与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.

解决方法

clock()计算cpu时间,因此如果它等待输入,它不会添加任何时间.

猜你在找的Xcode相关文章