【问题标题】:measuring performance using chrono, order seems to matter although it shouldn't使用chrono测量性能,顺序似乎很重要,尽管它不应该
【发布时间】:2019-08-19 10:32:53
【问题描述】:

作为一个练习,我试图衡量两种算法的效率,它们应该执行相同的任务,排序一个堆栈,只使用一个堆栈作为支持数据结构:

#include <stack>
#include <iostream>
#include <chrono>

std::stack<int> sortStack(std::stack<int>& inS){
  std::stack<int> tmpS;
  int tmpV=0;
  tmpS.push(inS.top());
  inS.pop();

  while(!inS.empty()){
    if(inS.top()>=tmpS.top()){
      tmpS.push(inS.top());
      inS.pop();
    }else{
      tmpV = inS.top();
      inS.pop();
      int count = 0;

      //reverse the stack until we find the item that is smaller
      while(!tmpS.empty()){
        if(tmpS.top()>tmpV){
          inS.push(tmpS.top());
          tmpS.pop();
          count++;
        }else{
          break;
        }
      }
      //tmpS.top is smaller (or =) than tmpV
      tmpS.push(tmpV);

      //and revert the other stack
      for(int i=0; i< count; i++){
        tmpS.push(inS.top());
        inS.pop();
      }
    }
  }

  return tmpS;
}

std::stack<int> sortStackRevisited(std::stack<int>& inS){
  std::stack<int> tmpS;
  int tmpV=0;

  while(!inS.empty()){
    tmpV = inS.top();
    inS.pop();
    //reverse the stack until we find the item that is smaller
      while(!tmpS.empty() && tmpS.top()>tmpV){
          inS.push(tmpS.top());
          tmpS.pop();
      }
      tmpS.push(tmpV);
    }

  return tmpS;
}
int main(){

using namespace std::chrono;
  std::stack<int> s1({1,0,123,3,5,89,23,12,1000});

  std::stack<int> s({1,0,123,3,5,89,23,12,1000});

  auto t1 = high_resolution_clock::now() ;

  std::stack<int> ordered = sortStackRevisited(s);

  auto t2 = high_resolution_clock::now() ;

  std::stack<int> ordered2 = sortStack(s1);

  auto t3 = high_resolution_clock::now() ;

  std::cout<<"\n";
  std::cout<<duration_cast<microseconds>(t2-t1).count()<<" "<<duration_cast<microseconds>(t3-t2).count()<<"\n";
}

运行这个程序,我始终得到 t2 - t1 大于 t3- t2。如果我更改函数调用的顺序,即:

  auto t1 = high_resolution_clock::now() ;

  std::stack<int> ordered = sortStack(s);

  auto t2 = high_resolution_clock::now() ;

  std::stack<int> ordered2 = sortStackRevisited(s1);

  auto t3 = high_resolution_clock::now() ;

我仍然认为 t2 - t1 大于 t3- t2。怎么了?我错过了什么吗?

编译我使用g++ --std=c++11 sortStack.cpp

【问题讨论】:

  • Why is it faster to process a sorted array than an unsorted array? 的可能重复项。编辑:没关系,我以为你在同一个容器上工作了两次。
  • -O2 或 -O3 会发生什么?
  • 一般来说,当像这样计时,你想多次运行每个算法,测量,例如,运行算法 1000 次所需的总时间,然后计算一次的平均时间从那跑。尤其是这么小的数据集,每次运行都会很快,以至于只测量一次运行的结果基本上只是噪音......
  • 嗯,您必须记住,您(很可能)在现代操作系统上运行,该操作系统具有在现代超标量处理器之上运行的抢占式多任务处理。这里有各种各样的效果。第一次运行将预热缓存。由于您的数据和代码很小,仅此一项就可能导致任何后续运行显着加快(这就是为什么人们通常会丢弃前几次运行的结果以允许老化期)。
  • 也可能是程序的启动恰好持续占用足够的时间,以使您的线程在第一个算法被挂起之前到达它的开始。由于单个运行几乎不需要时间,例如,这可能导致第一次运行被分成两个心跳,而第二次运行则在一个心跳中运行。您在这里测量的是执行到达程序中某些点之间的间隔时间(包括从其他应用程序运行线程所花费的潜在时间),而不是 CPU 在您的进程中花费的实际周期数......

标签: c++ algorithm performance performance-testing chrono


【解决方案1】:

只运行一次代码对于基准测试来说是不可靠的。您的代码(在修复 main 以返回 int 之后)在我的机器上运行不到一微秒(使用 -O2,因为它应该被优化)。

如果我将其更改为运行代码 100000 次,我会得到 sortStack 比 sortStackRevisited 快的结果,无论它们以什么顺序运行。

运行sortStack先:30141 32997

运行sortStackRevisited先:31244 26642

即使在这里,您也可以看到仅运行 100k 测试时的变化。时间还不是很准确。

所以我假设您正在运行未优化的代码并且只运行一次,这可能有各种工件导致结果出现问题。其中之一是您正在测量实时结果,而不是 CPU 时间,这意味着操作系统向运行代码的 CPU 抛出的任何东西都会导致它在实时运行时变慢,即使 CPU 时间相同。或者可能导致 CPU 缓存以不同的方式处理事情,或者这个或那个。有很多可能性。

我的机器上未优化的代码运行速度比优化后的慢 20 倍,这表明编译器可以做很多事情来使事情变得更好并影响最终结果。

但是在将它们相互比较时,例如运行一百万次会产生相对较好的结果。这两个值会有所不同,但与彼此相比,它们会保持不变。例如,在我的机器上,时间除以另一个是 1.139-1.142,所以很明显另一种方法慢了 14%。如果我只运行 10 次,结果是运行速度会慢 200%。运行 1000 次时,速度会慢 12-28%。因此,正如统计数据所说,更多的尝试会带来更高的准确性。

【讨论】:

  • 嗨,萨米,感谢您的回答。你提出的观点正是我要问的。为什么也不可靠?造成问题的人工制品是什么?
猜你喜欢
  • 2015-03-25
  • 2014-12-18
  • 1970-01-01
  • 2014-09-06
  • 1970-01-01
  • 1970-01-01
  • 2020-04-23
  • 2017-03-04
  • 2014-07-17
相关资源
最近更新 更多