What is the correct way to clear Array in Haxe? -
what performant way clear array in haxe? assigning variable holding array empty one. , found on internet:
public static function clear(arr:array<dynamic>){ #if (cpp) arr.splice(0,arr.length); #else untyped arr.length = 0; #end } is best way? concerned 2 targets: js , cpp.
for part can use reassignment empty array clear array; becomes problematic if reference array important. in case, have works well.
that's answer, curiosity's sake, decided try timing of ways clear arrays. unfortunately, haven't used haxe in while , in computer's configurations must have changed, can compile neko , html5 @ moment. regardless, results interesting.
for test, ran 4 different clear algorithms through arrays ranging 8 1048576 integers in length. algorithms follows:
splice clear:
array.splice(0, array.length); length clear:
untyped array.length = 0; assignment clear:
array = []; pop clear:
while (array.length > 0) array.pop(); all times shown below represent total time taken perform same operation 1 million times.
in neko:
- splice: 0.51 seconds
- length: 0.069 seconds
- assignment: 0.34 seconds
- pop: 0.071 0.179 seconds (scales linearly array gets bigger)
in html5:
- splice: 0.29 seconds
- length: 0.046 seconds
- assignment: 0.032 seconds
- pop: 0.012 seconds
these tests run on 64-bit windows 7 machine , firefox.
i'm bit surprised while loop method fasted algorithm in javascript; makes me think going on there. otherwise, length method on platforms support it.
my tests on github in case wants peer review methods , perhaps try out tests on platforms other neko , html5.
Comments
Post a Comment