我有一个实验库,我正在尝试测量其性能。为此,我编写了以下内容:
struct timeval begin;
gettimeofday(&begin, NULL);
{
// Experiment!
}
struct timeval end;
gettimeofday(&end, NULL);
// Print the time it took!
std::cout << "Time: " << 100000 * (end.tv_sec - begin.tv_sec) + (end.tv_usec - begin.tv_usec) << std::endl;
有时,我的结果包括负时间,其中一些是荒谬的。例如:
Time: 226762
Time: 220222
Time: 210883
Time: -688976
这是怎么回事?