Hey folks,
I have a business case where i want to post a heart beat per second.
The problem is the code run slow is down by a few miliseconds that over time, it drags behind. I'd like 86400 seconds, but i'll end up short because of computation time.
Can this be solved?
Even in a simple case where i don't publish to my database it slows down.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
#include <thread>
#include <chrono>
#include <ctime>
using namespace std;
int main()
{
int maxcount=86400;
int a=0;
auto genesistime = std::chrono::system_clock::now();
std::time_t genesis = std::chrono::system_clock::to_time_t(genesistime);
cout<<"We are starting here: " << std::ctime(&genesis);
while(a<maxcount)
{
auto start = std::chrono::system_clock::now();
a++;
std::this_thread::sleep_for (std::chrono::seconds(1));
auto end = std::chrono::system_clock::now();
if (a % 3600==0)
{
cout<<a<<"-->";
std::chrono::duration<double> elapsed_seconds = end-start;
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
std::cout << "finished computation at " << std::ctime(&end_time)
<< "elapsed time: " << elapsed_seconds.count() << "s\n";
}
}
return 0;
}
|
##output
We are starting here: Wed May 22 10:49:16 2019
3600-->finished computation at Wed May 22 11:49:17 2019
elapsed time: 1.00012s
7200-->finished computation at Wed May 22 12:49:17 2019
elapsed time: 1.00008s
10800-->finished computation at Wed May 22 13:49:18 2019
elapsed time: 1.00008s
14400-->finished computation at Wed May 22 14:49:18 2019
elapsed time: 1.00008s
18000-->finished computation at Wed May 22 15:49:18 2019
elapsed time: 1.00006s
21600-->finished computation at Wed May 22 16:49:19 2019
elapsed time: 1.00007s
25200-->finished computation at Wed May 22 17:49:19 2019
elapsed time: 1.00007s
28800-->finished computation at Wed May 22 18:49:19 2019
elapsed time: 1.00007s
32400-->finished computation at Wed May 22 19:49:20 2019
elapsed time: 1.00012s
36000-->finished computation at Wed May 22 20:49:20 2019
elapsed time: 1.00007s
39600-->finished computation at Wed May 22 21:49:20 2019
elapsed time: 1.00007s
43200-->finished computation at Wed May 22 22:49:20 2019
elapsed time: 1.00013s