转帖:AS3.0性能优化

前段时间看见篇文章是说性能优化的,今天有时间好好看了一下,很多基础的,也有一些比较值得注意的,顺便把主要的一些结论翻译出来,对这方面不是太了解的朋友可以看一下,原文点这里
 
1. Avoid the new operator when creating Arrays
var a = [];
NOT:
var a = new Array();
不要new数组,使用[];
顺便说下FP10支持类型数组Vector,这里 有篇文章可以看下,是说Vector比Array快将近60%。
 

2. Arrays are expensive to create, do so conservatively
var vanityCollection01 : Array = new Array();
var vanityCollection02 : Array = new Array();
var vanityCollection03 : Array = new Array();
var vanityCollection04 : Array = new Array();
数组是非常耗费资源的,适当使用。
 
3. Fastest way to copy an array:
var copy : Array = sourceArray.concat();
复制数组最快的方法是用concat()。
 
4. Setting values in Arrays is slow
employees.push( employee );
employees[2] = employee;
给数组元素赋值是很慢的。
 
5. Getting values from Arrays is twice as fast as setting
var employee : Employee = employees[2];
从数组getting一个值速度是setting的两倍
 
6. Use static for properties methods that do not require an object instance

? View Code ACTIONSCRIPT3
StringUtils.trim(


 "text with space at end "


 )


;
Class definition:
package { public final class StringUtils
{
public static function trim( s : String ) : String { var trimmed : String ; // implementation... return trimmed; } } }

尽量使用静态属性和函数而不要依赖一个实例
特别是功能类的函数
 
7. Use const for properties that will never change throughout the lifecycle of the application
public const APPLICATION_PUBLISHER : String = “Company, Inc.”;
使用const申明那些程序里从来不会改变的属性变量
 
8. Use final when no subclasses need to be created of a class
public final class StringUtils
使用final定义不会有子类的类
 
9. Length of method/variable names doesn’t matter in ActionScript 3.0 (true in other langs)
someCrazyLongMethodNameDoesntReallyImpactPerformanceTooMuch();
长的变量/函数名是程序的执行是没有影响的。(其他语言也是)
 
10. One line assignments DO NOT buy any performance (true in other langs)
var i=0; j=10; k=200;
用一行声明若干变量不会提升性能。(其他语言也是)
 
11. No difference in memory usage between an if statement and a switch statement

? View Code ACTIONSCRIPT3
if

(

 condition )


{


     // handle condition


}



IDENTICAL MEMORY USAGE: switch ( condition )
{ case "A" :
// logic to handle case A
break ;
 
case
"B" : // logic to handle case B break ;
}

if和switch语句使用时在内存占用上是没有差别的。
 
12. Rank your if statements in order of comparisons most likely to be true

? View Code ACTIONSCRIPT3
if

(

conditionThatHappensAlot )


{


     // logic to handle frequently met condition


}


else

 if

(

conditionThatHappensSomtimes )


{
    // handle the case that happens occaisonally


}



else { // handle the case that doesn’t happen that often }

把为true概率大的条件放到前面。
 
13. AVM promotes int to Number during calculations inside loops (VM has been changing, from 9 to 10, so int, uint and number conversions aren’t as slow as they used to be.)
int, uint 和 number的转换不像以前那样慢。
 
15. Use uint sparingly, it can be slow (VM has been changing, from 9 to 10, so int, uint and number conversions aren’t as slow as they used to be.)
保守使用uint
 
16. Use integers for iterations
for(var i: int = 0; i < n; i++)
NOT
for (var i: Number = 0; i < n; i++)
循环中使用int而不是Number
 
18. Multiply vs. Divide: instead of 5000/1000 use: 5000*0.001 用乘*代替除/
 
19. Locally store function values in for and while statements instead of repeatedly accessing them
for (..){ a * 180 / Math.PI; }
declare: toRadians = a*180/Math.PI; outside of the loop
循环外面计算出循环中要使用的值存放在变量中在循环中使用而不是每次循环中去计算
 
20. Avoid calculations and method calls in loops
var len : int = myArray.lengh;
for (var i=0;i
NOT:
for (var i=0;i< myArray.lengh;i++){ }
在循环外面计算出循环次数(如果是动态的话)而不是每次循环重新计算。
这个问题我以前常犯-_-!!!
 
21. Use RegEx for validation, use string methods for searching

? View Code ACTIONSCRIPT3
// postal code validation example using regular expressions



private var regEx: RegExp = /^ [ A- Z] [ 0 - 9 ] [ A- Z] [ 0 - 9 ] [ A- Z] [ 0 - 9 ] $/ i;
private function validatePostal( event : Event ) : void { if ( regEx.test ( zipTextInput.text ) ) { // handle invalid input case } } // search a string using String methods var string : String = "Search me" ;
var searchIndex : int = string.indexOf ( "me" ) ;
var search : String = string.substring ( searchIndex, searchIndex + 2 ) ;

使用正则表达式来校验字符串,使用String的substring()方法来查找。
 
29. Remove unnecessary container wrappers to reduce container nesting
使用最少的容器嵌套。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值