需要对大量数据进行运算,选用哪种存储方式,效率最高,做了已下测试。
代码:
package { import flash.display.MovieClip; import flash.events.MouseEvent; import flash.utils.ByteArray; public class Main extends MovieClip { public function Main() { // constructor code stage.addEventListener(MouseEvent.CLICK, testTime); } /** * 测试运行效率,一维数组速度最快,对象次之,二维数组最慢 * @param e */ private function testTime(e:MouseEvent):void { trace("=========================================="); var i:int, j:int, a:Array,vec:Vector., bytes:ByteArray; var t:Number = new Date().getTime(); a = []; for (i = 0; i < 5000000; i++) { var arr:Array = []; a[i] = arr; arr[0] = i; arr[1] = i; a[i][0] * a[i][1]; } trace("二维数组用时:" + (-t + (t = new Date().getTime())) + "ms"); a = []; for ( i = 0; i < 5000000; i++) { var obj:Object = { }; a[i] = obj; obj.re = i; obj.im = i; a[i].re * a[i].im; } trace("Object对象用时:" + ( -t + (t = new Date().getTime())) + "ms"); a = []; for ( i = 0; i < 5000000; i++) { var mo:MyObject= new MyObject(); a[i] = mo; mo.re = i; mo.im = i; a[i].re * a[i].im; } trace("class对象用时:" + ( -t + (t = new Date().getTime())) + "ms"); a = []; for ( i = 0; i < 5000000; i++) { j=2*i; a[j]=i; a[j+1]=i; a[j]*a[j+1]; } trace("一维数组用时:" + ( -t + (t = new Date().getTime())) + "ms"); vec = new Vector.(5000000 << 1, true); for ( i = 0; i < 5000000; i++) { j=2*i; vec[j]=i; vec[j+1]=i; vec[j]*vec[j+1]; } trace("定长Vector用时:" + ( -t + (t = new Date().getTime())) + "ms"); vec = new Vector.(5000000 << 1, false); for ( i = 0; i < 5000000; i++) { j=2*i; vec[j]=i; vec[j+1]=i; vec[j]*vec[j+1]; } trace("不定长Vector用时:" + ( -t + (t = new Date().getTime())) + "ms"); bytes = new ByteArray(); for ( i = 0; i < 5000000; i++) { j=2*i; bytes.writeDouble(i); bytes.writeDouble(i); bytes.position -= 16; bytes.readDouble()*bytes.readDouble(); } trace("ByteArray用时:" + ( -t + (t = new Date().getTime())) + "ms"); } } } class MyObject { public var re:Number; public var im:Number; public function MyObject() { } }
运行结果(3次)如下:
========================================== 二维数组用时:3261ms Object对象用时:5860ms class对象用时:3054ms 一维数组用时:1127ms 定长Vector用时:97ms 不定长Vector用时:96ms ByteArray用时:2766ms ========================================== 二维数组用时:2118ms Object对象用时:5867ms class对象用时:3093ms 一维数组用时:1175ms 定长Vector用时:97ms 不定长Vector用时:96ms ByteArray用时:2628ms ========================================== 二维数组用时:2244ms Object对象用时:5943ms class对象用时:3111ms 一维数组用时:1432ms 定长Vector用时:124ms 不定长Vector用时:119ms ByteArray用时:3051ms
vector效率最高,然后是一维数组,其他就不考虑了。(二维数组比一维数组要慢的多)
顺便用js测试了一下。代码如下:
<html> <head> <title></title> <script> //测试运行效率,一维数组最快,对象次之, 二维数组最慢 console.log("------"); var a=[]; var t=0; var time=new Date().getTime(); for(var i=0;i<5000000;i++){ a[i]=[]; a[i][0]=i; a[i][1]=i; t=a[i][0]*a[i][1]; } console.log("二维数组用时:"+(-time+(time=new Date().getTime()))+"ms"); a=[]; for(var i=0;i<5000000;i++){ a[i]={}; a[i].re=i; a[i].im=i; t=a[i].re*a[i].im; } console.log("对象用时:"+(-time+(time=new Date().getTime()))+"ms"); a=[]; for(var i=0;i<5000000;i++){ var j=2*i; a[j]=i; a[j+1]=i; t=a[j]*a[j+1]; } console.log("一维数组用时:"+(-time+(time=new Date().getTime()))+"ms"); </script> </head> <body> </body> </html>
谷歌浏览器运行结果如下:
二维数组用时:3586ms 对象用时:2203ms 一维数组用时:318ms
一维数组最快,对象次之,二维数组最慢。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。