std::atan2与cv::fastAtan2计算速率比较

#include 
#include 
#include 
#include 
#include 

constexpr float PI_DIVIDED_BY_180 = M_PI / 180.0;

int main() {
  std::default_random_engine e;
  timeval tpstart1, tpend1, tpstart2, tpend2;
  int num = 10000;
  std::vector x, y;
  for(int i = 0; i < num; ++i) {
    x.push_back(e());
    y.push_back(e());
  }

  float res1;
  gettimeofday(&tpstart1, NULL);
  for(int i = 0; i < num; ++i) {
    res1 = std::atan2(y[i], x[i]);
  }
  gettimeofday(&tpend1, NULL);

  float res2;
  gettimeofday(&tpstart2, NULL);
  for(int i = 0; i < num; ++i) {
    res2 = cv::fastAtan2(y[i], x[i]) * PI_DIVIDED_BY_180;
  }
  gettimeofday(&tpend2, NULL);


  float timeuse1 = (1000000 * (tpend1.tv_sec - tpstart1.tv_sec)
      + tpend1.tv_usec - tpstart1.tv_usec);
  float timeuse2 = (1000000 * (tpend2.tv_sec - tpstart2.tv_sec)
      + tpend2.tv_usec - tpstart2.tv_usec);

  std::cout << "std::atan2 use time: " << timeuse1 << " us, result: " << res1 << std::endl;
  std::cout << "cv::fastAtan2 use time: " << timeuse2 << " us, result: " << res2 <<  std::endl;

  return 0;
}

几次测试的输出结果:

std::atan2 use time: 568 us, result: 0.405707
cv::fastAtan2 use time: 161 us, result: 0.405781

std::atan2 use time: 514 us, result: 0.405707
cv::fastAtan2 use time: 183 us, result: 0.405781

std::atan2 use time: 323 us, result: 0.405707
cv::fastAtan2 use time: 114 us, result: 0.405781

你可能感兴趣的:(笔记)