Reputation: 1125
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
Reputation: 21
There are a few errors with this.
You cannot measure in NS timescale. Most computers don't support that. Instead test with ms since that IS supported by all modern computers.
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