0

How can I measure the execution time of a line of code in C++in Windows. I am inserting about 1,00,000 records boost::multi_index_container as follows:

while(...) //read a single record from a csv file until EOF
{
    ...
    while(...) // split the record into components based on delimiter
    {
        ...
    }
    //insert into boost::multi_index_container
} 

I need to find the time required to insert all the records, but without the execution time of loops. Starting a timer or anything just before insert function and calculating the elapsed time just after the function call gives 0 nanoseconds as the result. So I cannot calculate the time by summing up the individual times. What is the solution?

Jackzz
  • 1,417
  • 4
  • 24
  • 53
  • In principle, you could tackle it like this: 1. Measure time of an empty loop (but make sure it is not optimized away). 2. Measure the time of your insert loop. 3. Take the difference. Devil is in the detail though that if you want to measure, you should measure the optimized code, not debug code. And then, there is a chance, the optimizer makes it harder for you. Check the assembler output to make sure you measure what you think you measure. Since you do not want the file read times as well, you might come up with some additional steps and differences to finally get what you need. – BitTickler Jun 23 '16 at 06:23
  • I wonder why you need this measure it? – K117 Jun 23 '16 at 06:33
  • @pradyot: I want a solution for Windows system – Jackzz Jun 23 '16 at 06:55
  • @BitTickler: I will give a try..But not so sure that I understood what u meant.. – Jackzz Jun 23 '16 at 06:56
  • @K117: to measure performance – Jackzz Jun 23 '16 at 06:56
  • 1
    @Jackzz. it obvious. Why do you need to measure performance? – K117 Jun 23 '16 at 07:07
  • Suggestion: if you want to just profile (i.e., this is needed for dev and not for release), use valgrind. That measures clock cycles for you. – lorro Jun 23 '16 at 07:20

2 Answers2

2

On Windows, you can get accurate measurements with QueryPerformanceCounter.

goobliata
  • 340
  • 1
  • 7
  • QPC gives execution time as 0 for each single insert. As I have mentioned I need the total execution time to insert the one lakh records.. – Jackzz Jun 23 '16 at 06:53
  • You may need to measure CPU cycles somehow for something that fine-grained. – goobliata Jun 23 '16 at 06:57
  • As I mentioned above, if what you want to measure is too small, measure the time it takes to do it multiple times (so it takes a measurable amount of time). Then, the magic operation is "division": ``total_time / N`` if N is the number of times the operation has been performed. Since your loop and file access also adds to total_time, you need comparative runs of the same code without your insert operation. Then ``(total_time - total_time_compare) / N``. – BitTickler Jun 26 '16 at 11:18
0

possible duplicate of How to calculate a time difference in C++. There are many ways to do this. One that I like uses chrono.

   #include <iostream>
   #include <chrono>

   using namespace std;
   using namespace std::chrono;



   int main()
  {
    high_resolution_clock::time_point t1 = high_resolution_clock::now();
    //your code here
    high_resolution_clock::time_point t2 = high_resolution_clock::now();

    auto duration = duration_cast<microseconds>( t2 - t1 ).count();

    cout << duration;
    return 0;
  }
Community
  • 1
  • 1