基本類型

使用 getSize() 方法對程式碼進行基準測試,並判斷對於工作最有效率的物件。

除了 String 之外的所有基本類型都會使用 4 – 8 個位元組的記憶體。為基本類型使用特定類型將無法最佳化記憶體:

// Primitive types 
var a:Number; 
trace(getSize(a)); 
// output: 8 
 
var b:int; 
trace(getSize(b)); 
// output: 4 
 
var c:uint; 
trace(getSize(c)); 
// output: 4 
 
var d:Boolean; 
trace(getSize(d)); 
// output: 4 
 
var e:String;  
trace(getSize(e)); 
// output: 4

如果未指定值給 Number (代表 64 位元值),ActionScript Virtual Machine (AVM) 會配置 8 位元組給它。所有其他的基本類型都會儲存為 4 個位元組。

// Primitive types 
var a:Number = 8; 
trace(getSize(a)); 
// output: 4 
 
a = Number.MAX_VALUE; 
trace(getSize(a)); 
// output: 8

String 類型的行為會有所不同。配置的儲存量是以 String 的長度為基礎:

var name:String;         
trace(getSize(name)); 
// output: 4 
             
name = ""; 
trace(getSize(name)); 
// output: 24 
         
name = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularized in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";     
trace(getSize(name)); 
// output: 1172

使用 getSize() 方法對程式碼進行基準測試,並判斷對於工作最有效率的物件。