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快!

  • 相关阅读:
    Activity跳转动画
    如何查询自己的手机版本?欧版、亚太、港行、还是国行?
    VC程序员常用工具汇总
    陈灯可重用代码段管理器VS插件版5.0发布(代码段收集器、个人代码库、代码片段管理、代码管理)
    基于OSLC的系统集成
    安桌点菜源代码
    eclipse jsp 文字设置
    ubuntu 下 netbeans平台 使用C连接mysql
    使用gdi+实时绘制picturebox(画个叉)
    思路上的转变,运用投影和一阶导数的思想
  • 原文地址:https://www.cnblogs.com/tianlanliao/p/3434468.html
Copyright © 2011-2022 走看看