std::atan2와 cv:::fastAtan2의 계산 속도 비교

1620 단어 필기
#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

좋은 웹페이지 즐겨찾기