0

I know there are many topics for finding elapse time of a code and I have read many, but i really get confused I decided to use high resolution clock and ran this code to see how many times a second the clock ticks

cout << chrono::high_resolution_clock::period::den << endl;
output:10000000

and then I defined the begin time and the end time as below

int main()
{
    auto begin = std::chrono::high_resolution_clock::now();
    .
    .
    .
    .
    auto end = std::chrono::high_resolution_clock::now();
    cout<<"RUN TIME IS :"<<endl;
    std::cout << std::chrono:: duration_cast <chrono:: nanoseconds>(end-begin).count() << "ns" << std::endl;
    system("PAUSE");
    //**********************************************************
    return 0;
}

but most of the time the output is 0, but the code is more than 2000lines I waste a lot of time,but the problem has not solved yet . So please let me know is it a good way to determine the elapse time ? And what is the wrong with my code ? I am using VS2012 another question is that i want to measure a computation time of a problem and I am not sure which one should i measure(system time , user time or real time) ? Thank you in advance

IMI
  • 105
  • 1
  • 1
  • 9
  • This question is really urgent and I am not advance in c++ please guide me with your clear answers. – IMI Apr 20 '14 at 18:50
  • I can't be sure what you're asking. What you've shown [works](http://coliru.stacked-crooked.com/a/345b7d36dab164f5). – chris Apr 20 '14 at 18:51
  • I have omitted the main part of the code and the mentioned part is timing section, I asked for benchmarking and measuring computation time should I use high resolution clock ? and the code I prepared comes up with 0, while the code is more than 2000lines ?@chris – IMI Apr 20 '14 at 18:57
  • Ah, you deleted your own question for some reason, and then repeated it today. http://stackoverflow.com/questions/23170412/high-resolution-timer-error – Mats Petersson Apr 20 '14 at 18:59
  • 1
    @IMI:Please read my post on similar topics: http://stackoverflow.com/a/23116709/2724703 – Mantosh Kumar Apr 20 '14 at 19:01
  • @MatsPetersson yes because I havent found any answer – IMI Apr 20 '14 at 19:03
  • 1
    See my link above: It's basically that Windows implementation (at least in VS 2012) is "b0rked". There are various other ways to measure time in VS. And I wrote a comment to the original question explaining about system, user and real time. – Mats Petersson Apr 20 '14 at 19:05

1 Answers1

2

This is correct... But the clock is not actually required to be "high-resolution", that's implementation defined.

You may consider using an alternate method, which is built into the Windows API:

Check out the QueryPerformanceCounter APIs, as explained in this question.

Community
  • 1
  • 1
smiling_nameless
  • 1,047
  • 1
  • 9
  • 23
  • the topic was so useful, but I want to know how can I add an ending point for the timer? – IMI Apr 20 '14 at 19:37
  • 1
    First, sorry for the wait. Anyways, you don't need to define an ending point. The timer is normally implemented in hardware; you would calculate elapsed time by calling `QueryPerformanceCounter`, and subtracting the two `QuadParts`, divided by the value returned by `QueryPerformanceFrequency`. I don't know what you're thinking, but the timer doesn't start at 0, so you must always have an initial time to determine the difference between them. Understand? – smiling_nameless Apr 20 '14 at 22:02
  • Yes, fortunately the problem is solved by your clear answers. Thank you again – IMI Apr 21 '14 at 09:30