快速sin cos
来源:互联网 发布:淘宝优站官网 编辑:程序博客网 时间:2024/05/18 01:40
在优化算法时时间项费非常关键,如果需要大量使用sin cos,那么对sin cos的优化将大大提高运算速度
最近发现一个快速计算sin cos的方法,
#include"cmath"#include<iostream>#include<cstdio>#include<ctime>using namespace std;#define MILLION 1000000////////////////////////////////////////////////////////////////////////////////#define CV_PI 3.14159265359#define halfPi ((float)(CV_PI*0.5))#define Pi ((float)CV_PI)#define a0 0 /*-4.172325e-7f*/ /*(-(float)0x7)/((float)0x1000000); */#define a1 1.000025f /*((float)0x1922253)/((float)0x1000000)*2/Pi; */#define a2 -2.652905e-4f /*(-(float)0x2ae6)/((float)0x1000000)*4/(Pi*Pi); */#define a3 -0.165624f /*(-(float)0xa45511)/((float)0x1000000)*8/(Pi*Pi*Pi); */#define a4 -1.964532e-3f /*(-(float)0x30fd3)/((float)0x1000000)*16/(Pi*Pi*Pi*Pi); */#define a5 1.02575e-2f /*((float)0x191cac)/((float)0x1000000)*32/(Pi*Pi*Pi*Pi*Pi); */#define a6 -9.580378e-4f /*(-(float)0x3af27)/((float)0x1000000)*64/(Pi*Pi*Pi*Pi*Pi*Pi); */#define _sin(x) ((((((a6*(x) + a5)*(x) + a4)*(x) + a3)*(x) + a2)*(x) + a1)*(x) + a0)#define _cos(x) _sin(halfPi - (x))////////////////////////////////////////////////////////////////////////////////////int main(){ struct timespec tpstart; struct timespec tpend; long timedif;//fastsin cos{float sine, cose;float dsin, dcos;clock_gettime(CLOCK_MONOTONIC, &tpstart);for(float ang = 0.0; ang < CV_PI; ang += 0.0001){sine = _sin(ang);cose = _cos(ang);}clock_gettime(CLOCK_MONOTONIC, &tpend); timedif = MILLION*(tpend.tv_sec-tpstart.tv_sec)+(tpend.tv_nsec-tpstart.tv_nsec)/1000; printf("it took %ld microseconds\n", timedif);}//sin cos{double sine,cose;clock_gettime(CLOCK_MONOTONIC, &tpstart);for(double ang = 0.0; ang < CV_PI; ang += 0.0001){sine = sin(ang);cose = cos(ang);}clock_gettime(CLOCK_MONOTONIC, &tpend); timedif = MILLION*(tpend.tv_sec-tpstart.tv_sec)+(tpend.tv_nsec-tpstart.tv_nsec)/1000; printf("it took %ld microseconds\n", timedif);}return 0;}
Makefile文件
fastsin : fastsin.cppg++ -o fastsin fastsin.cpp -lrtclean:rm -f fastsin
计算结果:
it took 1300 microseconds
it took 6297 microseconds
系统自带的sin cos计算(double),大概是快速sin cos计算(float)时间的5倍。
- 快速sin cos
- 快速sin 和 cos 函数计算
- 快速sin()和cos()的实现
- 一種簡單,快速,精准的sin/cos函數模擬,及as3實現
- 练习,sin,cos多项式
- sin cos tan cot公式
- sin和cos的爱恋
- 输出sin,cos的值
- 三角函数sin cos模拟测试
- js数学 atan2 cos sin
- UVA 12575 Sin Cos Problem
- 太乐了,sin cos
- sin与cos的求法
- cos和sin的使用
- matplotlib——sin,cos
- 一种简单,快速,精准的sin/cos函数模拟,及as3实现
- 一种简单,快速,精准的sin/cos函数模拟,及as3实现
- 一种简单,快速,精准的sin/cos函数模拟,及as3实现
- 2011国信蓝点杯全国软件设计大赛
- C#生成随机字符串
- java实现把一个大文件切割成N个固定大小的文件
- 修改linux源
- Android从相册为个人App选取大图片并裁剪终极解决方案
- 快速sin cos
- 科研项目
- Understanding GC pauses in JVM, HotSpot's CMS collector.
- 三角形类
- 中文系统下使用Zend Server ce后显示错误页的解决
- 字符集的所有组合
- 从零开始--系统深入学习android(实践-让我们开始写代码-Android框架学习-7.通知)
- TomCat服务器应用基础
- Data Mining数据分析经典语录汇总