as3效率优化

有两种基准测试方式,分别是:定量分析(quantitative)、定性分析(qualitative).前者是可测量的,后者是可感知的。定量分析依赖测量工具。

#13-#14:定量分析方法1:测量消失的时间

 

view plain copy to clipboard print ?
  1. t=getTimer();   
  2. //一些测量的代码   
  3. elapsedTime = getTimer()-t;  

 

<div class='cnblogs_code'><img id="Code_Closed_Image_233112" onclick="this.style.display='none'; document.getElementById('Code_Closed_Text_233112').style.display='none'; document.getElementById('Code_Open_Image_233112').style.display='inline'; document.getElementById('Code_Open_Text_233112').style.display='inline';" height="16" src="/Images/OutliningIndicators/ContractedBlock.gif" width="11" align="top"><img id="Code_Open_Image_233112" style="display: none" onclick="this.style.display='none'; document.getElementById('Code_Open_Text_233112').style.display='none'; getElementById('Code_Closed_Image_233112').style.display='inline'; getElementById('Code_Closed_Text_233112').style.display='inline';" height="16" src="/Images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top"><span id="Code_Closed_Text_233112" class='cnblogs_code_Collapse'>Code</span><span id="Code_Open_Text_233112" style="display: none"><br><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">var&nbsp;intTime&nbsp;:&nbsp;Number;<br>&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;numberTime&nbsp;:&nbsp;Number;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;i&nbsp;:&nbsp;</span><span style="color: #0000FF;">int</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;j&nbsp;:&nbsp;</span><span style="color: #0000FF;">int</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">0</span><span style="color: #000000;">;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;intTime&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000FF;">new</span><span style="color: #000000;">&nbsp;Date()).time;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF;">for</span><span style="color: #000000;">&nbsp;(i</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">;&nbsp;i</span><span style="color: #000000;">&lt;</span><span style="color: #800080;">10000000</span><span style="color: #000000;">;&nbsp;i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;j&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(j&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">15</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">/</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">7</span><span style="color: #000000;">;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;intTime&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000FF;">new</span><span style="color: #000000;">&nbsp;Date()).time&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;intTime;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;n&nbsp;:&nbsp;Number;<br>&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;m&nbsp;:&nbsp;Number&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">0</span><span style="color: #000000;">;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;numberTime&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000FF;">new</span><span style="color: #000000;">&nbsp;Date()).time;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF;">for</span><span style="color: #000000;">&nbsp;(n</span><span style="color: #000000;">=</span><span style="color: #800080;">0</span><span style="color: #000000;">;&nbsp;n</span><span style="color: #000000;">&lt;</span><span style="color: #800080;">10000000</span><span style="color: #000000;">;&nbsp;n</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(m&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">15</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">/</span><span style="color: #000000;">&nbsp;</span><span style="color: #800080;">7</span><span style="color: #000000;">;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;numberTime&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(</span><span style="color: #0000FF;">new</span><span style="color: #000000;">&nbsp;Date()).time&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;numberTime;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;message&nbsp;:&nbsp;String&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">int&nbsp;version:&nbsp;</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;intTime&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">ms\n</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">Number&nbsp;version:&nbsp;</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;numberTime&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">ms</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;trace(message);<br></span></span></div>t=getTimer(); //一些测量的代码 elapsedTime = getTimer()-t;

 

#15-#19:方法2:利用健壮的测试框架来测试。

#20:测试时间一定要足够长,使得得出的结果很明显(ps:例如运行10万次循环之类。),同时避免在程序刚运行不久就测试。

========================

http://gskinner.com/talks/quick/#1

 

 

AS3效率优化——Boost Up

今天上The Flash Blog的时候,发现了gskinner弄了一个华丽丽滴flash幻灯片。讲授的自然是flash的事情咯。主题是提高代码效率。
原flash地址:http://gskinner.com/talks/quick/
这个真是值得所有fla...

 

 

(续)AS3效率优化——preso翻译记录中(1-41)

上一篇日志已经说过gskinner发布了一个讲AS3效率的幻灯片。于是乎,偶比较好事地开始翻译记录XD。非重点的都会掠过。
#1:跳过(主要是偶不懂"I‘m grant"究竟是啥意思。。。)
#2-#4:...

 

(续)AS3效率优化——preso翻译记录(42-81)

~续上一贴~
#42:#43开始便是一些优化例子。
#43:减少常数出现的次数(collapse literal values)
var a:uint = b+(1024-200)/2;
var a:uint = b+412;
应取后者。效率分析数据

...

 

(续)AS3效率优化——preso翻译记录(82-100)

~续前一篇~
#82:下面讲讲多媒体方面的优化
#83:总的来说,所有媒体都是占用CPU滴~所以,不用的时候记得关上~
#84:视频优化:减少播放面积,减低帧速,减小关键帧出现频率(...

===================================================

 

 

as3效率优化

在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.7763568394002505 e - 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.7763568394002505 e - 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

一些相关的链接:
Dennis Ippel – Some ActionScript 3.0 Optimizations and links

Andre Michelle – General Guidelines for AS3 Optimizations

  1. AS3 optimations & suggestions
  2. ActionScript 3.0 and AVM2: Performance Tuning
  3. Resource management strategies in Flash Player 9
  4. Understanding garbage collection in Flash Player 9
  5. gskinner.com: gBlog: Types in AS3: ints not so fast, uints slow!
  6. Actionscript optimization resources
  7. More performance tuning in Actionscript 3
  8. Bitwise gems – fast integer math
  9. AS3 and AVM Performance Tuning tips from Gary Grossman
  10. AS3: Rethink your old assumptions
  11. ActionScript 3 Performance Tuning by Matt Chotin
  12. Fast and accurate sine/cosine approximation
  13. Optimizations for AS3 calculations

 

你可能感兴趣的:(as3效率优化)