关于卡马克算法和系统函数库方法快速开平方根快慢的研究
2016-12-08 01:13
441 查看
class MyMath { public: float GetSqrtMy(float num); float GetSqrtSys(float num); private : long i; float x2, y; const float threehalfs = 1.5F; };
#include "MyMath.h"
#include <iostream>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<cfloat>
float MyMath::GetSqrtMy(float number)
{
x2 = number * 0.5F;
y = number;
i = *(long *)&y; // evil floating point bit level hacking
i = 0x5f3759df - (i >> 1); // what the fuck?
y = *(float *)&i;
y = y * (threehalfs - (x2 * y * y));
y = y * (threehalfs - (x2 * y * y));
y = y * (threehalfs - (x2 * y * y));
return y*number;
}
float MyMath::GetSqrtSys(float number)
{
float sq = sqrt(number);
return sq;
}
#include "MyMath.h"
#include <iostream>
#include <ctime>
int main()
{
clock_t startTime, endTime;
MyMath* myMath = new MyMath();
int res = 0;
long average = 0l;
for (int count = 0; count < 10; count++)
{
startTime = clock();
for (int i = 0; i < 100000000; i++)
{
res = myMath->GetSqrtSys(float(i));
}
endTime = clock();
average += endTime - startTime;
}
printf("res = %d,start = %ld, end = %ld,sys = %ld\n", res, startTime, endTime, average / 10);
average = 0l;
for (int count = 0; count < 10; count++)
{
startTime = clock();
for (int i = 0; i < 100000000; i++)
{
res = myMath->GetSqrtMy(float(i));
}
endTime = clock();
average += endTime - startTime;
}
printf("res = %d,start = %ld, end = %ld, my = %ld\n", res, startTime, endTime, average / 10);
}
release编译结果(10次求平均值):
x86
x64
但是下午在公司电脑上测试的,当release时,卡马克的更快,大概快不到一倍,在debug,系统函数要快。但是在我自己电脑上却是上面的结果。很奇怪。
相关文章推荐
- 关于JSF应用程序生命周期与Creator2阶段方法的深入研究
- 关于findcontrol()方法的一个研究
- 汉语词典快速查询算法研究
- 关于通达度的研究及全国尺度的通达度计算方法
- 关于Cygwin环境下快速搭建GTK+2.0开发环境的方法(原创)
- 关于Global.asa文件的深入研究与session变量失效提示的具体方法
- 关于数值方法的一些算法解析(2)
- [转贴]《雷神之锤III》里求平方根倒数的函数(快速平方根(倒数)算法)
- C#关于参数为null(空值)的方法调用,重载顺序选择彻底研究
- 关于JAVA继承类的静态变量、成员变量、父子类构造方法调用顺序的研究
- 卡马克算法-代码研究
- 关于Global.asa文件的深入研究与session变量失效提示的具体方法
- 汉语词典快速查询算法研究
- 快速求平方根的算法
- 关于tomcat快速启动及自动重启的方法与脚本
- 关于findcontrol()方法的一个研究
- 关于Global.asa文件的深入研究与session变量失效提示的具体方法
- 准备学习和研究FIR和IIR 算法优化方法
- 快速开平方的算法
- 关于数值方法的一些算法解析(1)