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

#include <vector>
#include <iostream>
#include <sys/time.h>
#include <opencv2/opencv.hpp>
#include <random>

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<float> 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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值