加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex处理大数据量时,因该注意的一些事

发布时间:2020-12-15 01:11:09 所属栏目:百科 来源:网络整理
导读:项目中因为有遇到大数据量时的性能问题,今天稍作总结: 1.ArrayCollection,Array,Vector 插入比较 ? ?场景:向三个集合中分别插入100w条数据 ? ?代码://insert public function compareInsert():void { trace("------------------------------------------
项目中因为有遇到大数据量时的性能问题,今天稍作总结: 1.ArrayCollection,Array,Vector 插入比较 ? ?场景:向三个集合中分别插入100w条数据 ? ?代码://insert public function compareInsert():void { trace("-------------------------------------------------------"); arrCollection = new ArrayCollection(); arr = []; vector = new Vector.<PerformanceVo>(); queryItem = getObject("colName",queryIndex); var pv:PerformanceVo; var colName:String = "ArrayCollection"; tStart = getTimer(); for(i=0;i<total;i++){ if(queryIndex == i){ pv = queryItem; }else{ pv = getObject(colName,i); } arrCollection.addItem(pv); } tEnd = getTimer(); trace("-----compareInsert-----ArrayCollection add "+total+" items takes time(ms): "+(tEnd - tStart)); colName = "Array"; tStart = getTimer(); for(i=0;i<total;i++){ if(queryIndex == i){ pv = queryItem; }else{ pv = getObject(colName,i); } arr.push(pv); } tEnd = getTimer(); trace("-----compareInsert-----Array---------- add "+total+" items takes time(ms): "+(tEnd - tStart)); colName = "Vector"; tStart = getTimer(); for(i=0;i<total;i++){ if(queryIndex == i){ pv = queryItem; }else{ pv = getObject(colName,i); } vector.push(pv); } tEnd = getTimer(); trace("-----compareInsert-----Vector--------- add "+total+" items takes time(ms): "+(tEnd - tStart)); } ? ?运行结果: ------------------------------------------------------- -----compareInsert-----ArrayCollection add 1000000 items takes time(ms): 20673 -----compareInsert-----Array---------- add 1000000 items takes time(ms): 3067 -----compareInsert-----Vector--------- add 1000000 items takes time(ms): 2937 结论:ArrayCollection使用addItem()、Array使用push()、Vector使用push()分别添加100w条数据时:Array与Vector的速度差不多平均情况下,可 能Array稍微快一点,而ArrayCollection最慢,几乎比Array和Vector慢了近7倍; 2.ArrayCollection,Vector 查询比较 场景:在前边三个集合中分别在同一位置加入了queryItem子项,现查找出改对象在集合中的index; 代码: //query public function compareQuery():void { trace("-------------------------------------------------------"); var tempPV:int; tStart = getTimer(); tempPV = arrCollection.getItemIndex(queryItem); tEnd = getTimer(); trace("-----compareQuery-----ArrayCollection-----getItemAt "+tempPV+" items takes time(ms): "+(tEnd - tStart)); tStart = getTimer(); tempPV = arr.indexOf(queryItem); tEnd = getTimer(); trace("-----compareQuery-----Array-----------------indexOf "+tempPV+" items takes time(ms): "+(tEnd - tStart)); tStart = getTimer(); tempPV = vector.indexOf(queryItem); tEnd = getTimer(); trace("-----compareQuery-----Vector----------------indexOf "+tempPV+" items takes time(ms): "+(tEnd - tStart)); } 运行结果: ------------------------------------------------------- -----compareQuery-----ArrayCollection-----getItemAt 654321 items takes time(ms): 161 -----compareQuery-----Array-----------------indexOf 654321 items takes time(ms): 12 -----compareQuery-----Vector----------------indexOf 654321 items takes time(ms): 12 结论:ArrayCollection的getIndexAt()速度最慢,在100w条数据时,比Array、Vector的indexOf()慢了将近14倍;Array、Vector的indexOf()查找速度比较接近; 3.ArrayCollection,Vector 删除比较 结论ArrayCollection在大数据量是,通过removeAll,或者removeItemAt还是比较耗时的,所以我们建议这三者删除全部时采用以下方式: ArrayCollection a: ?a = new ArrayCollection(),或者a.source = []; Array b: b = []; Verctor c : c = new Vectory(); 鉴于以上的这些个结论,我们引申出以下的比较: 1.ArrayCollection的三种插入数据方式 场景:分别用addItem(),source.push(),source+disableAutoUpdate对ArrayCollection增加100w条数据; 代码: //ArrayCollection public function compareInsertOfArrayCollection(arrDispather:ArrayCollection,arrNotDispather:ArrayCollection,arrUpdateOneTime:ArrayCollection):void { trace("-------------------------------------------------------"); var obj:PerformanceVo; tStart = getTimer(); for(i=0;i<total;i++){ obj = ?getObject("ask"+i,i); arrDispather.addItem(obj); } tEnd = getTimer(); trace("-----compareInsertOfArrayCollection-----insert-----addItem-------------------items takes time(ms): "+(tEnd - tStart)); tStart = getTimer(); for(i=0;i<total;i++){ obj = ?getObject("bbb"+i,i); arrNotDispather.source.push(obj); } tEnd = getTimer(); trace("-----compareInsertOfArrayCollection-----insert-----source.push---------------items takes time(ms): "+(tEnd - tStart)); tStart = getTimer(); arrUpdateOneTime.disableAutoUpdate(); for(i=0;i<total;i++){ obj = ?getObject("aaa"+i,i); arrUpdateOneTime.source.push(obj); } arrUpdateOneTime.enableAutoUpdate(); tEnd = getTimer(); trace("-----compareInsertOfArrayCollection-----insert-----source+disableAutoUpdate--items takes time(ms): "+(tEnd - tStart)); return; } 运行结果: ------------------------------------------------------- -----compareInsertOfArrayCollection-----insert-----addItem-------------------items takes time(ms): 39050 -----compareInsertOfArrayCollection-----insert-----source.push---------------items takes time(ms): 5179 -----compareInsertOfArrayCollection-----insert-----source+disableAutoUpdate--items takes time(ms): 5077 结论:ArrayCollection添加数据时,以上三种插入方式的速度各有差异:addItem最慢,大概比source.push和 source+disableAutoUpdate慢了近8倍;当然如果还需要监听arraycollection的collectionchangeEvent的话,此时source.push中还需要手动派发事件,手动派发事件可以参考以下方法,切记:手动派发事件只能派发add和update事件,而如果手动派发remove事件,会报the index is invalid错误: public function arrayCollectionDispatcherChangeEvent(arr:ArrayCollection,items:Array,kind:String=CollectionEventKind.ADD):void { var collectEvent:CollectionEvent = new CollectionEvent(CollectionEvent.COLLECTION_CHANGE); collectEvent.kind = kind; if(kind == CollectionEventKind.UPDATE){ var pc:PropertyChangeEvent = new PropertyChangeEvent(PropertyChangeEvent.PROPERTY_CHANGE); pc.source = items[0]; collectEvent.items = [pc]; }else{ collectEvent.items = items; } arr.dispatchEvent(collectEvent); } 2.for与for each的速度比较 场景:通过不同的循环模式,寻找arrayCollection的中的特定元素 代码: //compare loop format public function compareLoopFomat():void { trace("-------------------------------------------------------"); var temp:Object; tStart = getTimer(); for(i=0;i<arrCollection.length;i++){ temp = arrCollection[i]; if(temp == queryItem){ break; } } tEnd = getTimer(); trace("-----compareLoopFomat-----for-arrayCollection[i]-----------items takes time(ms): "+(tEnd - tStart)); tStart = getTimer(); for(i=0;i<arrCollection.length;i++){ temp = arrCollection.source[i]; if(temp == queryItem){ break; } } tEnd = getTimer(); trace("-----compareLoopFomat-----for-arrayCollection.source[i]----items takes time(ms): "+(tEnd - tStart)); tStart = getTimer(); for(i=0;i<arrCollection.length;i++){ temp = arrCollection.getItemAt(i); if(temp == queryItem){ break; } } tEnd = getTimer(); trace("-----compareLoopFomat-----for-arrayCollection.getItemAt(i)-items takes time(ms): "+(tEnd - tStart)); tStart = getTimer(); for each(temp in arrCollection){ if(temp == queryItem){ break; } } tEnd = getTimer(); trace("-----compareLoopFomat-----for each-------------------------items takes time(ms): "+(tEnd - tStart)); } 运行结果: ------------------------------------------------------- -----compareLoopFomat-----for-arrayCollection[i]-----------items takes time(ms): 7964 -----compareLoopFomat-----for-arrayCollection.source[i]----items takes time(ms): 1870 -----compareLoopFomat-----for-arrayCollection.getItemAt(i)-items takes time(ms): 3496 -----compareLoopFomat-----for each-------------------------items takes time(ms): 3687 结论:由3和4的结果可以看出for 比for each 要快,从整体情况可以看出,大数据量是,用for + arrayCollection.source[i]的循环模式速度最快 ps.因为项目中用的是DataGrid进行展示,所以在性能不得不提起DataGrid,依据项目中的经验,最好不要自定义的它的列排序,个人在项目中因为自 定义了某些列的排序,使得在大数据量时,程序超过了默认的15脚步运行时间隔(虽然这个值可以调节);因此大数据量是不到万不得已,绝不自 定义DataGrid列的排序功能。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读