user1075940
user1075940

Reputation: 1125

delta time always zero

long long delta;
auto oldTime = std::chrono::high_resolution_clock::now();
std::vector<int> list;
for (int i = 0; i < 100; i++)
{

    auto x = i * i / std::pow((double)i / 50, 2) ;
    list.push_back(x);
    auto now = std::chrono::high_resolution_clock::now();
    delta = std::chrono::duration_cast<std::chrono::nanoseconds>(now - oldTime).count();
    oldTime = now;
    std::cout << delta << std::endl;
}

delta is suppose to show how much ns took to compute,insert and show result of simple equation, however some of results are equals 0.. how entire iteration can take 0ns ??

Upvotes: 0

Views: 539

Answers (1)

Celestialkey
Celestialkey

Reputation: 21

There are a few errors with this.

  1. You cannot measure in NS timescale. Most computers don't support that. Instead test with ms since that IS supported by all modern computers.

  2. Printing data to the console screen is a slow process. Since you are printing the data in the loop, it is going to slow the entire iteration way down, by a good 6-20 ms per loop instead of being almost instantaneous. Move the print call outside the loop for the most accurate results Just divide the total time by the number of iterations to see the time required for each loop.

Upvotes: 2

Related Questions