Actionscript 3 sorting performance

Recently I decided to look more into performance and optimization of Actionscript 3/Flash, mostly to learn what to do and what not when writing AS3 code.

A quick Google search gave me only few, outdated and not very reliable articles, so I decided to write few tests and to share the findings on my blog.

The tests

In this post I’m going to share the results of a first set of benchmarks aimed at testing the performance of several sorting functions available in AS3 for Arrays and Vectors.

The benchmarks are divided in 3 main groups, based on the data placed in the containers:

  • int
  • String
  • object

All the tests are ran on 3 different containers:

  • Array
  • Vector
  • Fixed size Vector

each container is filled with 50,000 randomly generated elements.

For all the tests times are in ms. and they are the average of 5 runs (spikes are not considered in the average), lower values are better.

The environment

The tests were executed on a 64-bit Windows 7 machine powered by an Intel i7-4770 CPU and 8Gb of DDR3 RAM.

The SWF has been generated with Flashdevelop 4.5.2 using Flex 4.6.0, obviously the configuration used to build is “Release”.

The Flash player used to run the tests is the latest available at the time of writing this post: 11,9,900,170.

Test group 1: int

For these tests the containers are filled with randomly generated ints.

CompareInt and CompareDescInt are two functions passed to sort() to compare two ints, the former is used for sorting them in ascending order, whereas the latter for sorting them in descending order.

Array Vector Vector (fixed)
sort() 426
sort(Array.NUMERIC) 8 27 33
sort(CompareInt) 49 68 52
sort(Array.DESCENDING) 418 421 463
sort(CompareDescInt) 49 50 53

Few things emerge from these data:

  • Array is the best container for sorting ints.
  • The fastest way to sort ints is using sort(Array.NUMERIC).
  • Using a custom function to sort ints is very slow (6.25 times slower than the fastest sort).
  • NEVER use sort(Array.DESCENDING) to sort ints in a descending order, always sort the container and then call reverse() on it (which on average took 0 ms. on my data).
  • NEVER use sort() with ints, something stupid is happening when you do that.

Test group  2: String

For these tests the containers are filled with randomly generated Strings of 16 characters.

CompareStr and CompareDescStr are two functions passed to sort() to compare two Strings, the former is used for sorting them in ascending order, whereas the latter for sorting them in descending order.

Array Vector Vector (fixed)
sort() 55
sort(Array.CASEINSENSITIVE) 433 467 442
sort(CompareStr) 58 58 59
sort(Array.DESCENDING) 29 29 30
sort(CompareDescStr) 80 80 80

Few things emerge from these data:

  • Array is the best container for sorting Strings.
  • Even if it’s hard to believe, apparently the fastest way to sort Strings is calling sort(Array.DESCENDING) and then reverse().
  • Do not use sort(Array.CASEINSENSITIVE) if not really needed.
  • Using a custom function to sort Strings is pretty slow (2 times slower than the fastest sort)

Test group 3: Object

For these tests the containers are filled with (relatively big) objects containing each: 11 ints, 1 Number, 1 String, 1 Shape and few functions.

CompareObj and CompareDescObj are two functions passed to sort() to compare an int value of two objects, the former is used for sorting them in ascending order, whereas the latter for sorting them in descending order.

Array Vector Vector (fixed)
sort(CompareObj) 159 158 185
sort(CompareDescObj) 157 157 158
sortOn(int) 13
sortOn(String) 34
sortOn(int, String) 139

Few things emerge from these data:

  • Array is the best container for sorting objects.
  • When sorting objects always use sortOn(…) to get the best results.
  • Using a custom function to sort objects is extremely slow (12.23 times slower than the fastest sort)

Conclusions

It’s clear that when you need to sort many elements your container of choice is the Array and that (not surprisingly) sorting ints is faster than sorting Strings or bigger objects, but using sortOn makes possible to obtain good performance with any kind of data.

Live Demo

If you want to run the benchmarks on your machine you can open this page.

Feel free to post your results as comment to this post.

Source Code

The whole Flashdevelop project is available as ZIP archive and can be downloaded here.

The source code is released under the “do whatever you want” license 😉 attribution and a link to this post would be appreciated though.

 

4 Comments

  1. zworp

    I get very different results (osX 10.8.4, Flash 12.0.9):

    ==== ARRAY ====
    Array(int) created in: 2 ms.
    Array(int).sort(): 415 ms.
    Array(int).sort(NUM): 8 ms.
    Array(int).sort(f): 46 ms.
    Array(int).sort(DES): 436 ms.
    Array(int).sort(fDES): 45 ms.
    Array(int).reverse(): 0 ms.

    Array(Str) created in: 2 ms.
    Array(Str).sort(): 34 ms.
    Array(Str).sort(INS): 448 ms.
    Array(Str).sort(f): 64 ms.
    Array(Str).sort(DES): 33 ms.
    Array(Str).sort(fDES): 88 ms.
    Array(Str).reverse(): 0 ms.

    Array(Obj) created in: 2 ms.
    Array(Obj).sort(f): 161 ms.
    Array(Obj).sort(fDES): 167 ms.
    Array(Obj).reverse(): 0 ms.
    Array(Obj).sortOn(int): 12 ms.
    Array(Obj).sortOn(Str): 42 ms.
    Array(Obj).sortOn(int,Str): 138 ms.

    ==== VECTOR ====
    Vector(int) created in: 2 ms.

    Vector(int).sort(NUM): 8 ms.
    Vector(int).sort(f): 46 ms.
    Vector(int).sort(DES): 448 ms.
    Vector(int).sort(fDES): 46 ms.
    Vector(int).reverse(): 0 ms.

    Vector(Str) created in: 2 ms.

    Vector(Str).sort(INS): 445 ms.
    Vector(Str).sort(f): 64 ms.
    Vector(Str).sort(DES): 34 ms.
    Vector(Str).sort(fDES): 88 ms.
    Array(Str).reverse(): 0 ms.

    Vector(Obj) created in: 2 ms.
    Vector(Obj).sort(f): 169 ms.
    Vector(Obj).sort(fDES): 179 ms.
    Vector(Obj).reverse(): 0 ms.

    ==== VECTOR(fixed) ====
    Fix Vector(int) created in: 1 ms.

    Fix Vector(int).sort(NUM): 8 ms.
    Fix Vector(int).sort(f): 31 ms.
    Fix Vector(int).sort(DES): 445 ms.
    Fix Vector(int).sort(fDES): 46 ms.
    Fix Vector(int).reverse(): 0 ms.

    Fix Vector(Str) created in: 2 ms.

    Fix Vector(Str).sort(INS): 453 ms.
    Fix Vector(Str).sort(f): 67 ms.
    Fix Vector(Str).sort(DES): 35 ms.
    Fix Vector(Str).sort(fDES): 88 ms.
    Fix Vector(Str).reverse(): 0 ms.

    Fix Vector(Obj) created in: 2 ms.
    Fix Vector(Obj).sort(f): 166 ms.
    Fix Vector(Obj).sort(fDES): 175 ms.
    Fix Vector(Obj).reverse(): 0 ms.

    Reply
    1. r00t (Post author)

      I can’t see any difference in the trends.

      Reply
  2. Yop

    my results

    ==== ARRAY ====
    Array(int) created in: 1 ms.
    Array(int).sort(): 426 ms.
    Array(int).sort(NUM): 10 ms.
    Array(int).sort(f): 50 ms.
    Array(int).sort(DES): 413 ms.
    Array(int).sort(fDES): 34 ms.
    Array(int).reverse(): 0 ms.

    Array(Str) created in: 2 ms.
    Array(Str).sort(): 36 ms.
    Array(Str).sort(INS): 458 ms.
    Array(Str).sort(f): 57 ms.
    Array(Str).sort(DES): 36 ms.
    Array(Str).sort(fDES): 81 ms.
    Array(Str).reverse(): 0 ms.

    Array(Obj) created in: 2 ms.
    Array(Obj).sort(f): 70 ms.
    Array(Obj).sort(fDES): 68 ms.
    Array(Obj).reverse(): 0 ms.
    Array(Obj).sortOn(int): 16 ms.
    Array(Obj).sortOn(Str): 43 ms.
    Array(Obj).sortOn(int,Str): 117 ms.

    ==== VECTOR ====
    Vector(int) created in: 3 ms.

    Vector(int).sort(NUM): 23 ms.
    Vector(int).sort(f): 50 ms.
    Vector(int).sort(DES): 415 ms.
    Vector(int).sort(fDES): 33 ms.
    Vector(int).reverse(): 0 ms.

    Vector(Str) created in: 1 ms.

    Vector(Str).sort(INS): 476 ms.
    Vector(Str).sort(f): 59 ms.
    Vector(Str).sort(DES): 40 ms.
    Vector(Str).sort(fDES): 79 ms.
    Array(Str).reverse(): 0 ms.

    Vector(Obj) created in: 2 ms.
    Vector(Obj).sort(f): 67 ms.
    Vector(Obj).sort(fDES): 73 ms.
    Vector(Obj).reverse(): 0 ms.

    ==== VECTOR(fixed) ====
    Fix Vector(int) created in: 0 ms.

    Fix Vector(int).sort(NUM): 10 ms.
    Fix Vector(int).sort(f): 38 ms.
    Fix Vector(int).sort(DES): 449 ms.
    Fix Vector(int).sort(fDES): 30 ms.
    Fix Vector(int).reverse(): 0 ms.

    Fix Vector(Str) created in: 1 ms.

    Fix Vector(Str).sort(INS): 460 ms.
    Fix Vector(Str).sort(f): 59 ms.
    Fix Vector(Str).sort(DES): 38 ms.
    Fix Vector(Str).sort(fDES): 78 ms.
    Fix Vector(Str).reverse(): 1 ms.

    Fix Vector(Obj) created in: 2 ms.
    Fix Vector(Obj).sort(f): 67 ms.
    Fix Vector(Obj).sort(fDES): 69 ms.
    Fix Vector(Obj).reverse(): 0 ms.

    Reply
  3. TheRabbit

    Win7 x64, i7 4770, 16 RAM dual channel, FP: 12.0.0.39

    ==== ARRAY ====
    Array(int) created in: 1 ms.
    Array(int).sort(): 268 ms.
    Array(int).sort(NUM): 8 ms.
    Array(int).sort(f): 27 ms.
    Array(int).sort(DES): 268 ms.
    Array(int).sort(fDES): 26 ms.
    Array(int).reverse(): 0 ms.

    Array(Str) created in: 2 ms.
    Array(Str).sort(): 28 ms.
    Array(Str).sort(INS): 272 ms.
    Array(Str).sort(f): 45 ms.
    Array(Str).sort(DES): 31 ms.
    Array(Str).sort(fDES): 63 ms.
    Array(Str).reverse(): 0 ms.

    Array(Obj) created in: 3 ms.
    Array(Obj).sort(f): 58 ms.
    Array(Obj).sort(fDES): 56 ms.
    Array(Obj).reverse(): 0 ms.
    Array(Obj).sortOn(int): 13 ms.
    Array(Obj).sortOn(Str): 34 ms.
    Array(Obj).sortOn(int,Str): 100 ms.

    ——————————————–

    ==== VECTOR ====
    Vector(int) created in: 1 ms.

    Vector(int).sort(NUM): 8 ms.
    Vector(int).sort(f): 27 ms.
    Vector(int).sort(DES): 269 ms.
    Vector(int).sort(fDES): 27 ms.
    Vector(int).reverse(): 0 ms.

    Vector(Str) created in: 1 ms.

    Vector(Str).sort(INS): 274 ms.
    Vector(Str).sort(f): 46 ms.
    Vector(Str).sort(DES): 31 ms.
    Vector(Str).sort(fDES): 65 ms.
    Array(Str).reverse(): 0 ms.

    Vector(Obj) created in: 2 ms.
    Vector(Obj).sort(f): 61 ms.
    Vector(Obj).sort(fDES): 59 ms.
    Vector(Obj).reverse(): 0 ms.

    ———————————————–

    ==== VECTOR(fixed) ====
    Fix Vector(int) created in: 0 ms.

    Fix Vector(int).sort(NUM): 8 ms.
    Fix Vector(int).sort(f): 18 ms.
    Fix Vector(int).sort(DES): 274 ms.
    Fix Vector(int).sort(fDES): 25 ms.
    Fix Vector(int).reverse(): 0 ms.

    Fix Vector(Str) created in: 1 ms.

    Fix Vector(Str).sort(INS): 272 ms.
    Fix Vector(Str).sort(f): 43 ms.
    Fix Vector(Str).sort(DES): 31 ms.
    Fix Vector(Str).sort(fDES): 65 ms.
    Fix Vector(Str).reverse(): 0 ms.

    Fix Vector(Obj) created in: 2 ms.
    Fix Vector(Obj).sort(f): 58 ms.
    Fix Vector(Obj).sort(fDES): 57 ms.
    Fix Vector(Obj).reverse(): 0 ms.

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *