简体   繁体   English

为什么在根据测量位置测量经过时间时会有很大差异?

[英]Why are there a big difference when measuring elapsed time according to where to measure?

My question is about the difference of the elapsed time according to the point.我的问题是关于根据点的经过时间的差异。

For finding the largest portion of the total elapsed time when executing in my code, I used clock function.为了找到在我的代码中执行时总运行时间的最大部分,我使用了clock函数。

source : calculating time elapsed in C++来源: 在 C++ 中计算经过的时间

First, I put the clock function at the start and end of the main function.首先,我把clock函数放在main函数的开头和结尾。 (Actually, there are some declaration of variables but I deleted them for readability of my questions). (实际上,有一些变量声明,但为了我的问题的可读性,我删除了它们)。 Then I think I will be able to measure the total elapsed time.然后我想我将能够测量总经过的时间。

int main(){

    using clock = std::chrono::system_clock;
    using sec = std::chrono::duration<double>;
    const auto before = clock::now();

...

    std::cin >> a >> b;
    lgstCommSubStr findingLCSS(a,b,numberofHT,cardi,SubsA);

    const sec duration = clock::now() - before;

    std::cout << "It took " << duration.count() << "s in main function" << std::endl;

    return 0;
}

Second, I put the clock function at the class findingLCSS .其次,我将clock函数放在了findingLCSS类中。 This class is for finding longest common sub-string between two string.此类用于查找两个字符串之间的最长公共子字符串。 It is the class that actually do my algorithm.这是实际执行我的算法的类。 I write the code for finding it in its constructor.我编写了在其构造函数中查找它的代码。 Therefore, when making this class, it returns longest common sub-string information.因此,在制作此类时,它会返回最长公共子串信息。 I think this elapsed time will be the actual algorithm running time.我认为这个经过的时间将是实际的算法运行时间。

public:
    lgstCommSubStr(string a, string b, int numHT, int m, vector <int> ** SA):
        strA(a), strB(b), hashTsize(numHT), SubstringsA(SA),
        primeNs(numHT), xs(numHT),
        A_hashValues(numHT), B_hashValues(numHT),
        av(numHT), bv(numHT), cardi(m)
{

        using clock = std::chrono::system_clock;
        using sec = std::chrono::duration<double>;
        const auto before = clock::now();
...

        answer ans=binarySearch(a,b, numHT);

        std::cout << ans.i << " " << ans.j << " " << ans.length << "\n";


        const sec duration = clock::now() - before;

        std::cout << "It took " << duration.count() << "s in the class" << std::endl;

}

The output is as below.输出如下。

tool coolbox
1 1 3
It took 0.002992s in inner class
It took 4.13945s in main function

It means 'tool' and 'coolbox' have a substring 'ool'这意味着 'tool' 和 'coolbox' 有一个子字符串 'ool'

But I am confused that there is a big difference between two times.但我很困惑,两次之间存在很大差异。

Because the first time is total time and the second time is algorithm running time, I have to think its difference time is the elapsed time for declaration variables.因为第一次是总时间,第二次是算法运行时间,所以我不得不认为它的差异时间是声明变量经过的时间。

But it looks weird because I think declaration variables time is short.但这看起来很奇怪,因为我认为声明变量的时间很短。

Is there a mistake in measuring the elapsed time?测量经过的时间是否有误?

Please give me a hint for troubleshoot.请给我一个排除故障的提示。 Thank you for reading!感谢您的阅读!

Taking a snapshot of the time before std::cin >> a >> b;std::cin >> a >> b;之前的时间进行快照std::cin >> a >> b; leads to an inaccurate measurement as you're likely starting the clock before you type in the values for a and b .导致测量不准确,因为您可能在输入ab的值之前开始计时。 Generally you want to put your timing as close as possible to the thing you're actually measuring.通常,您希望时间尽可能接近您实际测量的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM