文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>AS3效率优化【转载】

AS3效率优化【转载】

时间:2008-03-12  来源:yedong

在osflash mailing list看到的一些关于as3效率优化的讨论,(其实很早就在email里面了,只是因为懒到今天才看到)。

AS3 Speed tests page:
http://osflash.org/as3_speed_optimizations#as3_speed_tests
记一些以后会用到的tips,关于效率比较测试请看上面的文章。

1.a += b 要比 a = a + b 快,同样,自增a++也比a = a + 1快,不过自减a–不是比a=a-1快。

2.在做除以2操作时,乘法比除法快,位运算更快. 但是不要位运算来操作Number类型的变量,因为它会将Number类型的数值转为整数类型。

for Int : a = b >>1 faster than a = b *.5 faster than a = b /2 ;
for Number : a = b *.5 faster than a = b /2 ;

3.取整操作时,用unit()或int()比用Math.floor()和Math.ceil()要快,其中用uint(n) 比Math.floor(n)要快10倍.
比如var test:uint = uint(1.5);要比var test:Number = Math.floor(1.5);快,而 var test:uint = uint(1.5)+1;要比var test:Number = Math.ceil(1.5);也快。
如果是 Math.floor(),用位运算(>>0) 比unit() 或int()更快。

4.取绝对值时,*-1 比 Math.abs要快.如var test:Number = n < 0 ? n * -1 : n;快于var test:Number = Math.abs(n);

5.n+n比n*2快。

6.Math.sqrt()的替代算法.

function sqrt(w:Number):Number
{
var thresh:Number = .00001;
var b:Number = w * 0.25,a:Number,c:Number;
do
{
c = w / b;
b = (b + c) * 0.5;
a = b - c;
if (a &lt; 0)
{
a = -a;
}
}
while (a&gt; thresh);
return b;
}

作者的测试数据表明这个算法比较快一点。

Actual value = 12.136309158883519
Testing threshold: 0.00001
Number of iterations for approximation: 6
Approximation: 12.13630915888352
Error in approximation: 1.7763568394002505e-15
- - - -
Math.sqrt - Mean over 20 loops
Math.sqrt() Test:  172.05
Approximation Test:  134.8
Empty Test:  4.05
Net timing results:
Math.sqrt() Test: 168
Approximation Test: 130.75

可我copy了测试code,在自己电脑测试数据确反应这个算法是慢的。

Actual value = 12.136309158883519
Testing threshold: 0.00001
Number of iterations for approximation: 6
Approximation: 12.13630915888352
Error in approximation: 1.7763568394002505e-15
- - - -
Math.sqrt - Mean over 20 loops
Math.sqrt() Test:  199.55
Approximation Test:  210.3
Empty Test:  4.9
Net timing results:
Math.sqrt() Test: 194.65
Approximation Test: 205.4
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载