2
  1. I need to measured elapsed time in ms

  2. I need to store the start time as a primitive type

  3. I need to retrieve the start time as a primitive type, when making the comparison to determine how much time has elapsed

Any suggestions?

I have C++17 and do not want to use any external libraries (like boost).

std::chrono would be fine if someone could explain to me how to convert the elapsed time to/from a primitive. I'm not very good at C++.

resolution accuracy is not important.. if it is off by tens of ms that's ok, I just need to implement a delay.. e.g. 100ms or 1.5s

ycomp
  • 8,316
  • 19
  • 57
  • 95
  • what means "not want to use any libraries" ? Does that exclude the standard library? WIth language only you cannot measure time afaik. You can start from here https://stackoverflow.com/a/27739925/4117728 and store the start time as primitive type, though frankly that requirement is a little odd – 463035818_is_not_an_ai Jan 05 '22 at 15:19
  • If you cannot use libraries, does that mean you cant use #include ? The real question is ofcourse what do you want to measure? Instead of adding code to your code to measure your code you can also use a profiler. – Pepijn Kramer Jan 05 '22 at 15:23
  • 1
    You might find std::chrono useful in this scenario. Checkout [high_resolution_clock](https://www.cplusplus.com/reference/chrono/high_resolution_clock/now/) example for inspiration. – blurryroots Jan 05 '22 at 15:43
  • 1
    Can you elaborate on why you want to store the time as a primitive type? That's an unusual constraint. – templatetypedef Jan 05 '22 at 18:17
  • 2
    ah sorry, standard library is ok - I meant nothing external.. like boost – ycomp Jan 06 '22 at 06:22
  • 1
    I want to store the time as a primitive due to the limitations of the application that I'm using C++ to build an add-on for – ycomp Jan 06 '22 at 06:22

2 Answers2

4

Because you are storing the start time, you should use std::chrono::system_clock. Its epoch is stable, and will not change with time. Other std::chrono clocks have an epoch that may change between runs of a program, and thus time points stored by one run and read back in by a subsequent run may not be consistent.

To get the current time in milliseconds:

auto now = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());

I like to use either a using directive or a namespace alias to make such code less verbose:

using namespace std::chrono;
auto now = time_point_cast<milliseconds>(system_clock::now());

To convert now into a signed 64 bit integral type:

auto now_i = now.time_since_epoch().count();

To convert now_i back into a time_point:

time_point<system_clock, milliseconds> prev{milliseconds{now_i}};

To compare time_points:

if (now > prev)
    ...

Everything above is in the header <chrono>:

#include <chrono>
Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
1

You can simply do this by:

double time = 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC;

See the below code for better understanding.

#include <iostream>
#include <time.h>

using namespace std;

int main() {
  double start = 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC;

  for(int i=0;i<1e9;i++);

  double end = 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC;

  double time_taken = end - start;
  cout << time_taken << "ms\n";
  
  return 0;
}

Include the header file:

#include <time.h> // include this
csgeek
  • 711
  • 6
  • 15