论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > Flash AS编程教程
Tag:2.0,3.0菜鸟,游戏,,cs,技巧,源码,,文本,文字,函数,音乐,随机,拖拽,asp,access,xml,mc,视频教程

字典和索引数组-提升性能小技巧

文章类别:Flash AS编程 | 发表日期:2008-10-6 18:40:29


当你在客户端循环从大量的数据搜索指定数据的时候,这里有一个你可以提升额外性能的小技巧-那就是进肯能的使用字典和索引数组

尝试着基于其属性寻找正确的对象,而不是在大量的数据间循环,如果使用字典或者是索引数组那么你可以快速的定位你所需要的数据而不需要任何的循环。字典和索引数组允许你在内存中存放 名-值对这样的索引,这样你就可以很快定位指定的数据

在AS中你可以使用Object对象或者是Array对象来快速的创建这两者:

 

var map : Object = new Object();
map[ key ] 
= value; 

OR

 

var map : Array = new Array();
map[ key ] 
= value; 

 在运行的时候要访问指定的对象,你只需要提供该对象的属性值就可以了:

 

mySavedValue = map[ key ]; 

字典类和索引数组非常类似,当然字典类更加强大,字典允许你使用复杂的类作为键。

 

var map : Dictionary = new Dictionary();

var key1 : Object 
= new Object();
var key2 : Sprite 
= new Sprite();
var key3 : UIComponent 
= new UIComponent();

map[ key1 ] 
= value1
map[ key2 ] 
= value2
map[ key3 ] 
= value3

我发现这一点你在处理多重循环的时候特别有用,例如为了不象这样来做:

 

for each ( var o1 : Object in myCollection1 )
{
    
for each ( var o2 : Object in myCollection2 )
    
{
        
if ( o2.id == o1.relatedId )
        
{
            
//do something with the data that matches
            break;
         }

     }

}

你可以这样做,而且执行速度更快:

 

var map : Object = new Object();

//first create a map
for each ( var o2 : Object in myCollection2 )
{
    map[ o2.id ] 
= o2;
}


//now, loop over the first collection and use the map
for each ( var o1 : Object in myCollection1 )
{   
    var foundObject : 
* = map[ o1.relatedId ];
    
//now do something with the found object
}

这样你只需要遍历一个结合创建MAP,然后遍历另一个集合来使用其属性来访问Map获得指定内容,而不像原来要一个两重的循环【时间复杂度就从 O(n^2)降到了O(n)】

当 合理的使用的使用这一技巧带来的性能提升,你可能自己都会感到吃惊,你可以用来查找打标签的函数,引用数据,创建数据映射,或者是任意的场景,需要循环遍 历大量数据,如果你能除掉循环,只需要在程序中创建一次引用然后任何地方都可以使用,这可以保证你不把CPU时间浪费在无意义的查找不正确的时间上

当然使用字典对象你也需要注意,在使用完之后你需要释放它,否则可能会造成内存泄漏

原文

 

Here's a quick tip to help you squeeze extra performance out of your Flex/ActionScript applications when looping over and crunching lots of data on the client side. Use the dictionary class or associative arrays when you can!

Rather than looping through lots of data, trying to find the right object based on its properties, you can use associative arrays to find what you are looking for quickly and easily, without looping over anything. Associative arrays (also know as hashes or maps) allow you to create key value-pairs used to create a lookup table for data/objects in memory.

In ActionScript, you can use either a generic object class or an array to create a simple map using string based keys for your object values. Here's an example:

var map : Object = new Object();
map[ key ] = value;
OR

var map : Array = new Array();
map[ key ] = value;
When you want to access your object at runtime, you just need to provide the key to retrieve the hashed value:

mySavedValue = map[ key ]; 
The dictionary class is very similar to an associative array, however a bit more powerful. The dictionary allows you to create maps based on complex objects as the keys for the key-value pair. For example:

var map : Dictionary = new Dictionary();

var key1 : Object = new Object();
var key2 : Sprite = new Sprite();
var key3 : UIComponent = new UIComponent();

map[ key1 ] = value1
map[ key2 ] = value2
map[ key3 ] = value3
I've found this to be really helpful in scenarios where you may have had nested loops to process data. Rather than doing something like this:

for each ( var o1 : Object in myCollection1 )
{
for each ( var o2 : Object in myCollection2 )
{
if ( o2.id == o1.relatedId )
{
//do something with the data that matches
break;
}
}
}
You could do this, which would execute much faster:

var map : Object = new Object();

//first create a map
for each ( var o2 : Object in myCollection2 )
{
map[ o2.id ] = o2;
}

//now, loop over the first collection and use the map
for each ( var o1 : Object in myCollection1 )
{
var foundObject : * = map[ o1.relatedId ];
//now do something with the found object
}
Rather than looping over an unknown amount of items in 2 collections, you just loop over one collection and build the map, then loop over the other collection and access the properties of the map.

You might be surprised the difference in performance that this can make when used properly. You can use this for specialized label functions, looking up reference data, creating data maps, or just about any scenario where you would have to loop over lots of data. If you can get away with it, only build the map once, then access it any time you can throughout the application; this will ensure that you aren't wasting cpu looping through data when you don't need be.

When using the dictionary class, you also need to be careful and clean up after yourself. Otherwise you can end up with memory leaks. You can check out the livedocs for more information.
视频教程列表
文章教程搜索
 
Flash AS推荐教程
Flash AS热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058