简体   繁体   中英

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 ??

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM