mercredi 17 avril 2019

std::this_thread::sleep_for() affect my benchmarch result

I'm benchmarking my code, and to simulate some behavior I will can sleep periodically.

pseudo code:

size_t total_time;
for (auto & q : query) {
    auto start = std::chrono::high_resolution_clock::now();
    // do the query
    auto end = std::chrono::high_resolution_clock::now();
    size_t t = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
    all_time += t;
    std::this_thread::sleep_for(std::chrono::milliseconds(50));

}

the strange thing is that when I comment out that sleep, all_time become faster. And I compute the total sleep time, it's roughly equal to the 'slower time' as if the sleep statement is moved into my time counting area....I use -O3 in my compiler, is it possible it do some strange optimization?

If I do want to sleep for duration to simulate the real situation, what's a reliable way to do so?

Aucun commentaire:

Enregistrer un commentaire