AS3 语法性能差异分析报告

AS3 语法性能差异分析报告

一句话提要:

此篇博文主要来用数据证明: AS3 语言中, 一样的逻辑, 用不同的语句表达方式来实现, 最终体现在性能上的差异. 

对您是否有用:

如果您的主要工作是 UI界面开发, 每帧中的"渲染"时间远大于"运算"时间, 此文对您似乎没有太大帮助.

如果您再开发一套游戏引擎, 或者利用as3之便做大量运算, 注意一下语法细节, 也许会给您的产品带来不少性能上的提升.

动手玩玩:

这里有一个运行实例, 您可以手动选择各种测试项, 设置循环次数, 然后在右侧"输出"里看到代码的真实运行时间

数据展示:

int VS uint
func:Function = function( a:Object )
{
    a = a + 1;
    a = a - 1;
    a = a * 2;
    a = a / 2;
};
1.    while(){};
2.    var a:int = 0; 
    while(){ func(a) };
3.    var a:uint = 0;
    while(){ func(a) };
序号 测试项 运行时间(毫秒) 时间百分比
1 4 0.1212
2 int 22 0.6667
3 uint 7 0.2121
 
for VS for_each
1. for( a in b ) {}
2. for each( a in b ) {}
3. for( a in b ){ c = b[a]; }
4. for each( a in b ){ c = b; }
序号 测试项 运行时间(毫秒) 时间百分比
1 for 15 0.1402
2 for each 4 0.0374
3 for ( fetch value ) 84 0.785
4 for each ( fetch value ) 4 0.0374

 

Dictionary weak VS not
1.    while(){}
2.    dic = new Dictionary( true );
    while(){ dic[i] = i; i = dic[i]; }
3.    dic = new Dictionary( false );
    while(){ dic[i] = i; i = dic[i]; }
序号 测试项 运行时间(毫秒) 时间百分比
1 null 12 0.0075
2 weak == true 760 0.4720
3 weak == false 838 0.5205

 

new Object() VS {}
1.    while() {}; 
2.    while() { obj = new Object(); }
3.    while() { obj = {}; }
4.    while() { 
        obj = new Object; 
        obj[ 'name1' ] = 1; 
    }
5.    while() { obj = {name1:1}; };
6.    while() { 
        obj = new Object; 
        obj[ 'name1' ] = 1; 
        obj[ 'name2' ] = 2;
    }
7.    while() { obj = {name1:1,name2:2};}
序号 测试项 运行时间(毫秒) 时间百分比
1 null 4 0.0011
2 Object 272 0.0718
3 Bracket - {} 268 0.0708
4 Object-add-property-1 765 0.2020
5 {name1:1} 658 0.1738
6 Object-add-property-2 969 0.2559
7 {name1:1,name2:2} 851 0.2247

 

a+b VS [a,b].join()
1.    while(){ str = 'abc'; }
2.    while(){ str = 'a' + 'b' + 'c'; }
3.    while(){ str = ['a','b','c'].join(); }
4.    arr = ['a','b','c'];    
    while(){ str = arr.join(); };
序号 测试项 运行时间(毫秒) 时间百分比
1 null 2 0.0019
2 a+b 1 0.0009
3 join 627 0.5954
4 join-with-init 423 0.4017
 
Math.abs VS a>0?a:-a
1.    while(){ num = init; }
2.    while(){ num = Math.abs( init ); }
3.    while(){ num = init > 0 ? init : -init; }
序号 测试项 运行时间(毫秒) 时间百分比
1 null 48 0.2105
2 Math.abs() 121 0.5307
3 num>0?num:-num 59 0.2588

 

 

 此项目可在 GitHub 上下载:

https://github.com/zjh1943/AS3-Performance-Test

原文地址:https://www.cnblogs.com/jhzhu/p/3086695.html