1

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.

source : calculating time elapsed in C++

First, I put the clock function at the start and end of the main function. (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. 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'

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!

nimdraks
  • 13
  • 3
  • First welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take [the SO tour](http://stackoverflow.com/tour), read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). And don't forget the *minimal* part of your [mcve]. – Some programmer dude Feb 04 '20 at 13:05
  • Then there must be several thousands of examples and documentaiton about all kinds of "longest substring" problems all over the Internet. Quite a few of them here. If you copy the title to this question into your favorite search engine, how many hits do you get then? How many of the top ten are relevant? – Some programmer dude Feb 04 '20 at 13:07
  • @Someprogrammerdude Thank you for your comment. I already searched about this at Google but I didn't get hints about my problem. But I have to try find it at Google as you say. – nimdraks Feb 04 '20 at 13:14
  • @Someprogrammerdude and I try to fix my question as your advice! thank you! – nimdraks Feb 04 '20 at 13:29
  • Are you compiling with optimization? If not you are wasting your time as C/C++ compilers are notorious for generating terrible assembler without it. – Adrian Cornish Feb 06 '20 at 13:58
  • 1
    `std::cin >> a >> b;` can certainly take a very long time, depending on where the input comes from. – aschepler Feb 06 '20 at 14:08
  • 1
    @AdrianCornish Yes, I did. I compile my code with "g++ -pipe -std=c++14 -lm -O2 -g3 -Wall -c -fmessage-length=0" at eclipse.Thanks! – nimdraks Feb 06 '20 at 14:10
  • @aschepler Aha I understand! When modifying the code by not using "cin", the total elapsed time is decreased properly. Please write your answer for adopting it. Thank you so much! – nimdraks Feb 06 '20 at 14:12
  • I'd also suggest maybe running your code for 100,000,000 iterations at least to time it, there are all sorts of things like CPU caches, disk caches etc which can affect the result – Adrian Cornish Feb 06 '20 at 14:13
  • @AdrianCornish Thank you for your advice! I add the iteration code right now as your advice. – nimdraks Feb 06 '20 at 14:14

1 Answers1

0

Taking a snapshot of the time before 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. Generally you want to put your timing as close as possible to the thing you're actually measuring.

Eyal Cinamon
  • 939
  • 5
  • 16