// you’re reading...

ActionScript 3.0

Speed Tests: Objects vs. Arrays vs. Dictionaries

I’ve been taking buses around Europe for a couple weeks now, and sometimes those trips get extremely long. Yesterday I took a bus from Venice to Seefeld (Austria), a trip that would have taken about five hours if not for the hurricane-like downpour they were having in Italy. Seriously, it was ridiculous- all traffic was on the side of the highway, and all the motorcyclists were huddled under the overpasses. But on the bright side, it did give me a lot of time to try out some things that I normally wouldn’t take the time to do, such as this experiment.

I make a lot of decisions daily based on things I think I know, but really have just always assumed. And so, on this bus ride, I decided to get to the bottom of a few of them. The first experiment I tried was with the read and write speeds of Objects, Arrays, and Dictionaries. In the past, I have always used Arrays when I need all the nifty sorting and organization methods, Dictionaries when I need fast access, object keys, or weak references, and Objects pretty much never (because they’re messy and slow). So yesterday I decided to see if my assumptions were actually valid, or if I had been doing things wrong this whole time.

So I set up a quick class to test. I created a Dictionary, an Object, and an Array, and then I put 100,000 integers in each one for the write test, and read them back out for the read test. This is what I got (all times are average and in milliseconds).

868
Object Array Dictionary
Read (integer key) 19 573
Write (integer key) 24 17 22
Read (string key) 586 N/A 573
Write (string key) 329 N/A 333

Obviously there are many more benchmarks that I could have run, but I also had a pretty good book on that bus. I think I’ve done enough experimentation to prove that, once again, my assumptions are invalid.

Discussion

6 comments for “Speed Tests: Objects vs. Arrays vs. Dictionaries”

  1. What are dictionaries?

    Posted by Jens | July 8, 2008, 6:07 am
  2. The Dictionary class is a really simple class in AS3 that allows you to store objects just like an object or an array. The difference is that you can use anything you want as a ‘key.’

    In Arrays you have to use integers, in Objects you have to use Strings, and in Dictionaries you can use numbers, strings, or even other objects.

    Here’s some more info: Adobe LiveDocs

    Posted by Zack Jordan | July 10, 2008, 6:43 am
  3. It’s interesting to see actual times for these tests. I’ll tell you, though, although objects look butt-slow, their real power is in using them in data structures. An array may be a great all-purpose tool, but when you need a heap, tree, or some sort of sorted structure, an array will probably be much slower.

    One reason is that for a structure made of objects, each object can have pointers (references) to the next object in the structure. So, every time you add another object, it takes the same amount of time. In many arrays (I know in C and C++, but I’m not sure in AS3), the computer begins with an array of a definite length, and if you add another element past that (with a push function), it automatically makes a new array twice the length of the old, and copies the old one into the new one (this is not the case if YOU specify the length. But for a dynamic data structure, this is normally not the case). This can cause some major speed issues if your computer is unknowingly copying a 50,000 length array into a 100,000 length array.

    So be careful when choosing your data structure. You should pick arrays only when they best suit the function at hand. Many times, however, a dynamic data structure made from objects (sometimes arrays) will suit your needs better.

    Posted by Benjamin | July 12, 2008, 8:17 pm
  4. So, how does xml fit into this speed comparison? I know xml has been a standard option in Flash in the past, but with it’s support in AS3, does it have a running in the speed trials of data structuring?

    Posted by Actionscription | August 6, 2008, 1:31 am
  5. That’s a good question. I’ll have to add that to the trials.

    Posted by Zack Jordan | August 6, 2008, 8:05 am
  6. I mean to say “I know xml hasn’t been a standard option in Flash in the past”

    Posted by Actionscription | August 6, 2008, 2:54 pm

Post a comment

Twitterpated