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

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

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

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

在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时间浪费在无意义的查找不正确的时间上

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

 

Associative Arrays

| | Comments (2)
AddThis Social Bookmark Button
 
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.
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值