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

  • 相关阅读:
    009——数组(九) each list array_map array_walk array_walk_recursive
    008——数组(八)删除添加数组 得到数组键名键值
    laravel怎么获取到public路径
    laravel中composer镜像服务的方式
    logback的使用和logback.xml详解
    log4j-over-slf4j工作原理详解
    java
    【Log】logback指定配置文件(二)
    logback 中文手册
    logback logback.xml常用配置详解(三) <filter>
  • 原文地址:https://www.cnblogs.com/tianlanliao/p/3434468.html
Copyright © 2011-2022 走看看