zoukankan      html  css  js  c++  java
  • new tips

    老外的一篇文章(原文地址http://stackoverflow.com/questions/6647677/tips-for-efficient-as3-coding),有这么一段描述:

    Use [] and new Object(), instead of, new Array() and {}. It is at best 3 times faster. And is extremely common for the more costly operation to occur in loops.

    Generally speaking, the new keyword is just plain expensive.

    import flash.utils.getTimer;publicvar _time:Number;publicvar _simpleArrayTime:Number;publicvar buffer:Array;publicfunction testArray():void{
        trace("----------------Array Test--------------");
        _time = getTimer();for(var a:int=0; a <100000; a++)
            buffer =[];
        _simpleArrayTime = getTimer()- _time;
        trace("[] * 100000 :"+_simpleArrayTime.toPrecision(21));
    
        _time = getTimer();for(var b:int=0; b <100000; b++)
            buffer =newArray();
        _simpleArrayTime = getTimer()- _time;
        trace("new Array() * 100000 :"+_simpleArrayTime.toPrecision(21));
    
        _time = getTimer();for(var c:int=0; c <100000; c++)
            buffer =[];
        _simpleArrayTime = getTimer()- _time;
        trace("[] * 100000 :"+_simpleArrayTime.toPrecision(21));}publicvar objBuffer:Object;publicfunction testObject():void{
        trace("----------------Object Test--------------");
        _time = getTimer();for(var a:int=0; a <100000; a++)
            objBuffer ={};
        _simpleArrayTime = getTimer()- _time;
        trace("{} * 100000 :"+_simpleArrayTime.toPrecision(21));
    
        _time = getTimer();for(var b:int=0; b <100000; b++)
            objBuffer =newObject();
        _simpleArrayTime = getTimer()- _time;
        trace("new Object() * 100000 :"+_simpleArrayTime.toPrecision(21));
    
        _time = getTimer();for(var c:int=0; c <100000; c++)
            objBuffer ={};
        _simpleArrayTime = getTimer()- _time;
        trace("{} * 100000 :"+_simpleArrayTime.toPrecision(21));}publicfunction runTests(event:Event=null):void{
        testArray();
        testObject();

    }

    ----------------Array Test--------------
    [] * 100000 :82.0000000000000000000
    new Array() * 100000 :152.000000000000000000
    [] * 100000 :53.0000000000000000000
    ----------------Object Test--------------
    {} * 100000 :53.0000000000000000000
    new Object() * 100000 :36.0000000000000000000
    {} * 100000 :53.0000000000000000000



    但在我本地的三次测试结果是这样的(WIN7+FB4.6+SDK4.6):


    ----------------Array Test--------------
    [] * 100000 :42.0000000000000000000
    new Array() * 100000 :117.000000000000000000
    [] * 100000 :43.0000000000000000000
    ----------------Object Test--------------
    [SWF] E:yueyicodeworkspaceMyDemoin-debugMyDemoTwo.swf - 2,313 bytes after decompression
    {} * 100000 :31.0000000000000000000
    new Object() * 100000 :30.0000000000000000000
    {} * 100000 :30.0000000000000000000

    ----------------Array Test--------------
    [] * 100000 :46.0000000000000000000
    new Array() * 100000 :124.000000000000000000
    [SWF] E:yueyicodeworkspaceMyDemoin-debugMyDemoTwo.swf - 2,313 bytes after decompression
    [] * 100000 :46.0000000000000000000
    ----------------Object Test--------------
    {} * 100000 :27.0000000000000000000
    new Object() * 100000 :29.0000000000000000000
    {} * 100000 :28.0000000000000000000

    ----------------Array Test--------------
    [] * 100000 :45.0000000000000000000
    new Array() * 100000 :112.000000000000000000
    [] * 100000 :43.0000000000000000000
    ----------------Object Test--------------
    [SWF] E:yueyicodeworkspaceMyDemoin-debugMyDemoTwo.swf - 2,313 bytes after decompression
    {} * 100000 :28.0000000000000000000
    new Object() * 100000 :28.0000000000000000000
    {} * 100000 :30.0000000000000000000

    所以说,[]比new Array要快,但{}并不比new Object快!

  • 相关阅读:
    PAIP.img ROM文件提取APK
    paip.提升程序稳定性最佳实践
    paip.验证码识别序列号的反转
    paip.android APK安装方法大总结系统应用的安装
    paip.php调试脱离IDE VC59
    paip.声音按键音延迟的解决
    paip.提升效率几款任务栏软件vc59
    paip.android 读取docx总结
    paip.C#.NET多线程访问 toolStripStatusLabel VC421
    paip.Image对象出现“对象当前正在其他地方使用或者GDI+中发生一般性错误的解决
  • 原文地址:https://www.cnblogs.com/tianlanliao/p/3434468.html
Copyright © 2011-2022 走看看