Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most efficient way to loop in Haxe?

I couldn't find any information on actual performance differences between loops in Haxe. They mentioned that Vector has some speed optimizations due to it being fixed length. What is the best way to loop over objects? And does it depend on the iterable object (e.g. Array vs. Vector vs. Map)?

Why does Haxe have so little presence on SO? Every other language has this question answered 5 times over...

like image 458
loganjones16 Avatar asked Mar 04 '23 05:03

loganjones16


1 Answers

Since no one had done performance benchmarks that I have found, I decided to run a test so this info is available to future Haxe programmers.

First note: If you aren't running over the loop very often, it is so fast it has almost no effect on performance. So if it is easier to just use an Array, do it. Performance is only affected if you are running over the thing over and over and/or if it is really big.

Turns out that your best choice depends on mostly your data structure. I found that Arrays tend to be faster when you do the for each style loop instead of the standard for loop or while loop. At small sizes Arrays are essentially as fast as Vectors, so in most cases you don't need to worry about which one to use. However, if you are doing stuff with pretty massive Arrays, it would be very beneficial to switch over to a Vector. And if you use a Vector, using the standard for or a while loop is essentially equivalent (although while is a touch faster). Maps are also pretty fast, especially if you avoid the foreach loop.

To reach these conclusions, I first tested the loops under these conditions:

  1. Tested Array, Vector and Map (Map just for fun).
  2. Filled each one to be structure[i] = i where i in 0...size with sizes in [20, 100, 1000, 10000, 100000] so you can find the right size for you.
  3. Tested each data structure on each size using three for loop types
    for (i in 0...size)
    for (item in array)
    while (i < size)
    
    and inside each loop I performed a look up and assignment arr[i] = arr[i] + 1;
  4. Each loop type was inside its own loop for (iter in 0...1000) to get a more accurate read on how loops perform. Note that I just add together the times for each loop, I don't average or anything like that. So if an array took 12 seconds, it was really 12 / 1000 => 0.012 seconds to execute once on average.

Finally, here is my benchmark (run in Debug for neko in HaxeDevelop):

Running test on size 20:

for (i...20) x 1000
    Array : 0.0019989013671875
    Vector : 0
    Map : 0.00300025939941406

for each(i in iterable) x 1000
    Array : 0.00100135803222656
    Vector : 0.00099945068359375
    Map : 0.0209999084472656

while (i < 20) x 1000
    Array : 0.00200080871582031
    Vector : 0.00099945068359375
    Map : 0.0019989013671875


Running test on size 100:

for (i...100) x 1000
    Array : 0.0120010375976563
    Vector : 0.0019989013671875
    Map : 0.0120010375976563

for each(i in iterable) x 1000
    Array : 0.00600051879882813
    Vector : 0.00299835205078125
    Map : 0.0190010070800781

while (i < 100) x 1000
    Array : 0.0119991302490234
    Vector : 0.00200080871582031
    Map : 0.0119991302490234


Running test on size 1000:

for (i...1000) x 1000
    Array : 0.11400032043457
    Vector : 0.0179996490478516
    Map : 0.104999542236328

for each(i in iterable) x 1000
    Array : 0.0550003051757813
    Vector : 0.0229988098144531
    Map : 0.210000991821289

while (i < 1000) x 1000
    Array : 0.105998992919922
    Vector : 0.0170001983642578
    Map : 0.101999282836914


Running test on size 10000:

for (i...10000) x 1000
    Array : 1.09500122070313
    Vector : 0.180000305175781
    Map : 1.09700012207031

for each(i in iterable) x 1000
    Array : 0.553998947143555
    Vector : 0.222999572753906
    Map : 2.17600059509277

while (i < 10000) x 1000
    Array : 1.07900047302246
    Vector : 0.170999526977539
    Map : 1.0620002746582


Running test on size 100000:

for (i...100000) x 1000
    Array : 10.9670009613037
    Vector : 1.80499839782715
    Map : 11.0330009460449

for each(i in iterable) x 1000
    Array : 5.54100036621094
    Vector : 2.21299934387207
    Map : 20.4000015258789

while (i < 100000) x 1000
    Array : 10.7889995574951
    Vector : 1.71500015258789
    Map : 10.8209991455078


total time: 83.8239994049072

Hope that helps anyone who is worried about performance and Haxe and who needs to use a lot of loops.

like image 171
loganjones16 Avatar answered Mar 12 '23 00:03

loganjones16