Skip to content

Instantly share code, notes, and snippets.

@sonygod
Created April 11, 2013 12:28
Show Gist options
  • Select an option

  • Save sonygod/5362990 to your computer and use it in GitHub Desktop.

Select an option

Save sonygod/5362990 to your computer and use it in GitHub Desktop.
compare async and sync performance
package ;
/**
* ...
* @author sonygod
*/
import async.Build;
import async.Async;
import haxe.Timer;
class Test implements Build
{
@async(var ret: Array<Float>) static function bubblesort(array : Array<Float>) {
var swapping = false;
var temp : Float;
while (!swapping) {
swapping = true;
for (i in 0...array.length) {
if (array[i] > array[i+1]) {
temp = array[i+1];
array[i+1] = array[i];
array[i] = temp;
swapping = false;
}
}
}
return array;
}
@async(var ret: Array<Float>) static function asynchronous(){
var arry:Array<Float>;
[ arry] = bubblesort([1337, 1, -465, 3.141592653589793, 789, 69, 789, -132, 3.141592653589793, 465, 789, 0, 27]);
return arry;
}
static function bubblesortSync(array : Array<Float>) {
var swapping = false;
var temp : Float;
while (!swapping) {
swapping = true;
for (i in 0...array.length) {
if (array[i] > array[i+1]) {
temp = array[i+1];
array[i+1] = array[i];
array[i] = temp;
swapping = false;
}
}
}
return array;
}
public static function getResult(err:NodeErr,data:Array<Float>) {
trace(data);
trace("end"+(Timer.stamp()*1000-startTime));
}
public static var startTime:Float;
public static function main() {
trace("start" );
startTime = Timer.stamp() * 1000;
asynchronous(getResult);
var startTime2 = Timer.stamp() * 1000;
bubblesortSync([1337, 1, -465, 3.141592653589793, 789, 69, 789, -132, 3.141592653589793, 465, 789, 0, 27]);
trace("endSync " + (Timer.stamp() * 1000 - startTime2));
}
static inline function delay(ms:Int, cb){
platformDelay(ms, function(){ trace(ms+' passed'); cb(null); });
}
static inline function platformDelay(ms:Int, fun){
#if (cpp || neko || php) fun(); #else haxe.Timer.delay(fun, ms); #end
}
}
typedef NodeErr = Null<String>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment