首页 > 未分类 > var读写和function读写,get-set读写效率比较
2019
12-06

var读写和function读写,get-set读写效率比较

var读写和function读写,get/set读写效率比较

 

var 比 function快4倍左右,

get/set和function差不多

 

95
var 读: 567
var 写: 563
[SWF] D:\flexProject\testSpeed\bin-debug\testSpeed.swf - 2,091 bytes after decompression
function 读: 1860
function 写: 2117
get/set 读: 1927
get/set 写: 2119

//测试代码

package {
 import flash.display.Sprite;
 import flash.utils.getTimer;

 public class testSpeed extends Sprite
 {
  public function testSpeed()
  {
   var aone:speedTest=new speedTest()  ;
   var max:int=5000000;//50000000
   var old:int=getTimer();
   for (var i:int = 0; i < max; i+=1) {
   
   }
   
   //var forTime:int=getTimer()-old;
   old=getTimer();
   
   //trace(forTime);
   trace(old);
   
   //
   var t:int=0;
   for (i = 0; i < max; i+=1) {
    t=aone.testVar;
   }
   trace("var 读:",getTimer() - old);// - forTime);
   
   old=getTimer();
   for (i = 0; i < max; i+=1) {
    aone.testVar=t;
   }
   trace("var 写:",getTimer() - old);// - forTime);
   //
   old=getTimer();
   //
   for (i = 0; i < max; i+=1) {
    t=aone.testFunctionGet();
   }
   trace("function 读:",getTimer() - old);// - forTime);
   old=getTimer();
   for (i = 0; i < max; i+=1) {
    aone.testFunctionSet(t);
   }
   trace("function 写:",getTimer() - old);// - forTime);
   
   //
   old=getTimer();
   //
   for (i = 0; i < max; i+=1) {
    t=aone.testGet;
   }
   trace("get/set 读:",getTimer() - old);// - forTime);
   old=getTimer();
   for (i = 0; i < max; i+=1) {
    aone.testSet=t;
   }
   trace("get/set 写:",getTimer() - old);// - forTime);
  }
 }
}

 

//

package{
 public class speedTest{
  public function speedTest(){}  
  public var testVar:int = 0;
  private var _function:int = 0;
  private var _getset:int = 0;
  public function testFunctionGet():int{
   return this._function;
  }
  public function testFunctionSet(i:int):void{
   this._function = i;
  }
  public function set testSet(i:int):void{
   this._getset = i;
  }
  public function get testGet():int{
   return this._getset;
  }
  
 }
}

最后编辑:
作者:搬运工