Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

flash - AS3 Vector of Arrays

Is there any performance benefit in storing Arrays in a Vector of type Array?

e.g Option 1

private var _arrays:Vector.<Array> =  new Vector.<Array>(2); 
_arrays[0] = new Array(10);
_arrays[1] = new Array(10);

Option 2

private var _arrays:Array =  new Array(2); 
_arrays[0] = new Array(10);
_arrays[1] = new Array(10);

Also can I have a Vector or Vectors?

 private var _vectors:Vector.<Vector> = new Vector.<Vector>(2);

_vectors[0] = new Vector.<String>(10);
_vectors[1] = new Vector.<String>(10);

Thanks,

Mark

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

EDIT

My original answer was wrong except for the very last part, and I have to apologize for that. I knew for a fact that Vector has exactly four implementations "under the hood". (You can find decompiled sources from FP 10 playerglobal.swc in a post by Robert Penner here) Three of those are for number types (int, uint and Number). One is for Object types. This last one serves as a catch-all and takes in all classes derived from Object. This is why I assumed that Vector.<Object> was still faster than Array, relying on the information regarding vectors and arrays available from Adobe.

However, it seems that this information is wrong, or at least it leaves out some important parts:

  1. While Vector.<AnyClassDerivedFromObject> allows for strict typing, this type information is only evaluated at compilation time (so you get more type safety), but not at runtime - thus essentially the benefits of strict typing object vectors do not apply to performance. See this blog post for more info.

  2. Consequently, the only implementations of Vector that are faster than Array are the ones for number types (!).

In fact, I have done some extensive testing on this, and have come to the conclusion that while Vector.<int>is up to 60% faster than Array of ints, all the derivates of Vector.<Object> are not only equal in speed (i.e. Vector.<Object> performs the same as Vector.<String>, they also are about 20% slower than Array. I've double- and triple-checked this, so I believe the results to be fairly accurate.

It still is true that the number type vectors are faster, so you should use those for performance benefits over Array. But:

END EDIT

Only if you're going to use sort(), sortOn() or any other of the convenient sorting functions of Array, you might still decide otherwise, because these are native functions, and as such really fast. Implementing your own sorting methods on a Vector will probably not match their speed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...