Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <chrono>
- #include <iostream>
- #include <thread>
- #include <vector>
- #include <numeric>
- using namespace std::chrono;
- typedef system_clock testclock;
- //typedef high_resolution_clock testclock;
- //Determine our platform's tic period
- const double microsPerClkTic{
- 1.0E6 * testclock::period::num / testclock::period::den };
- //Our REQUIRED processing period
- const milliseconds intervalPeriodMillis{ 10 };
- //The number of periods we'll run our test fixture for
- const int32_t numLoops{ 3000 + 1 };
- void periodicTask() {
- std::cout << "clock precision = "
- << microsPerClkTic
- << " microseconds/tic"
- << std::endl
- << "Desired Wakeup Period = "
- << intervalPeriodMillis.count()
- << " milliseconds"
- << std::endl
- << "Num Wakeups = "
- << numLoops - 1
- << std::endl;
- //Initialize the chrono timepoint & duration objects we'll be
- //using over & over inside our sleep loop
- testclock::time_point currentStartTime{ testclock::now() };
- testclock::time_point nextStartTime{ currentStartTime };
- //For storing our errors
- std::vector<testclock::duration> wakeupErrors{};
- wakeupErrors.reserve(numLoops);
- int32_t loopNum{}; //loop counter
- //Let's ROCK!!!!!!
- while (loopNum < numLoops) {
- //Get our current "wakeup" time
- currentStartTime = testclock::now();
- //Store the wakeup error for this pass
- wakeupErrors.push_back(currentStartTime - nextStartTime);
- //Determine the point in time at which we want to wakeup for the
- //next pass through the loop.
- nextStartTime =
- currentStartTime + intervalPeriodMillis;
- //Sleep till our next period start time
- std::this_thread::sleep_until(nextStartTime);
- ++loopNum;
- } //end while
- //Print the average wakeup error
- testclock::duration sum{};
- sum = std::accumulate(std::begin(wakeupErrors) + 1,
- std::end(wakeupErrors),
- sum);
- std::cout << "\nAverage Wakeup Error = "
- << duration_cast<microseconds>(sum).count() / (numLoops - 1)
- << " microseconds\n";
- }
- int main() {
- periodicTask();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement