3

This might sound like a basic question but I've searched a lot. I am trying to time-profile a function call in C++ & need to log the time in seconds up to 3 decimal places. For example 2.304 seconds or .791 seconds. I am trying to use std::chrono to do it like this:

auto start_time = std::chrono::system_clock::now();
DoSomeOperation();
std::chrono::duration<double> elapsed_time = std::chrono::system_clock::now() - start_time;
double execution_time = elapsed_time.count();

std::cout << "execution_time = " << execution_time << std::endl;

Following is the output I am getting:

execution_time = 1.9e-05
execution_time = 2.1e-05
execution_time = 1.8e-05
execution_time = 1.7e-05

I am sure that DoSomeOperation only takes a few milliseconds to complete & I need the number in seconds. I need the number in double to use it in a different calculation.

How can I convert this weird 1.9e-05 into a sensible number in double which yields in seconds like .304 or .067 ?

Trying the code from here, I've the same problem.

Community
  • 1
  • 1
TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
  • Move it a few magnitudes up. I recommend first checking, what time unit the method `now` returns, and do a conversion. NOTE: I think it returns time in seconds, but I am not sure – Top Sekret Nov 20 '16 at 15:30
  • 4
    `1.9e-05` is not weird. It means _multiply `1.9` by ten to the power `-5`_. – ForceBru Nov 20 '16 at 15:35
  • @ForceBru hehe. thanks for detailing that out. – TheWaterProgrammer Nov 20 '16 at 15:40
  • So as the execution times are in microseconds you'll only get zeroes out of this in seconds in three decimals. But if you're trying some other code timings then it might show something – Sami Kuhmonen Nov 20 '16 at 15:50

2 Answers2

3

To change the output format, try std::fixed and std::setprecision

double execution_time = 0.01234;
std::cout << "execution_time = "
          << std::fixed << std::setprecision(3)
          << execution_time << std::endl;

If you have several places where you need to output the execution time, it can be converted to a string and then re-used:

double execution_time = 0.01234;
std::stringstream stream;
stream << std::fixed << std::setprecision(3) << execution_time;
std::string execution_time_as_string = stream.str();

std::cout << "execution_time = " << execution_time_as_string << std::endl;
AlexD
  • 32,156
  • 3
  • 71
  • 65
  • Can you please edit your answer to do the changes on `execution_time` before the `std::cout`. I need to use the variable `execution_time` in some other places in my program as well – TheWaterProgrammer Nov 20 '16 at 15:33
  • @SegmentationFault Sorry, I did not understand. What I'm trying to say is that you just need proper formatting option to avoid scientific notation in the output. – AlexD Nov 20 '16 at 15:38
  • Reducing the accuracy of the internal number is silly. Use whatever resolution it has, and round the result to milliseconds only for display. However if you must have milliseconds, then take the float value, multiply by 1000, and store it in an integer type as milliseconds. Display as seconds by converting it where you must display as seconds. – Kenny Ostrom Nov 20 '16 at 15:41
  • @AlexD My question was that : How can apply `std::fixed` and `std::setprecision(3)` on `execution_time` without the `std::cout` ? – TheWaterProgrammer Nov 20 '16 at 15:51
  • Oh, you mean how to round it up? It could be done with help of `round`, but it is better to keep the data and just change the way you output it, as **@KennyOstrom** mentioned. – AlexD Nov 20 '16 at 15:56
  • 1
    @SegmentationFault Just added outputting to a string, you could re-use it in several places. – AlexD Nov 20 '16 at 16:06
2

Use

cout.setf(ios::fixed,ios::floatfield);
cout.precision(3);
cout << MyDouble;
Vincent Thacker
  • 393
  • 1
  • 13