measuring elapsed seconds using chrono (stop watch) C++

Viewed 118

I am currently trying to create a way to display the elapsed seconds (not the difference between cycles). My code is following:

#include <iostream>
#include <vector>
#include <chrono>
#include <Windows.h>

typedef std::chrono::high_resolution_clock::time_point TIME;
#define TIMENOW() std::chrono::high_resolution_clock::now()
#define TIMECAST(x) std::chrono::duration_cast<std::chrono::duration<double>>(x).count()

int main()
{
std::chrono::duration<double>  ms;
double t = 0;
while (1)
{

 TIME begin = TIMENOW();
 int c = 0;
 for (int i = 0; i < 10000000; i++)
 {
     c += i*100000; 
 }
 TIME end = TIMENOW();

 ms= std::chrono::duration_cast<std::chrono::duration<double>>(end - begin);
 
 t =t+ ms.count();
  std::cout << t << std::endl;

 }

I expected adding the delta time over and over again to roughly give me the elapsed time in seconds, however I noticed that only if I do i < big number it sort of is fairly accurate. If its only 10,000 or so, t seems to accumulate slower and gradually faster. Maybe I am missing something but isnt the difference my delta time(the elapsed time between this and last cycle) and if I keep adding the delta times up, it should spit out seconds? Any help is appreciated.

0 Answers
Related