Created
January 23, 2012 16:36
-
-
Save back2dos/1664135 to your computer and use it in GitHub Desktop.
Tinkerbell chunk iteration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ; | |
import haxe.FastList; | |
import haxe.Timer; | |
import tink.util.Benchmark; | |
import tink.util.ChunkIter; | |
import FL; | |
using tink.util.ChunkIter; | |
class IterTest { | |
static inline var count = | |
#if js | |
10000000;//give it up for chrome! | |
#elseif php | |
50000;//sad but true | |
#else | |
1000000; | |
#end | |
static public function run() { | |
#if (flash || js) | |
Timer.delay(arr, 100); | |
Timer.delay(fl, 1000); | |
Timer.delay(flc, 2000); | |
#else | |
arr(); | |
fl(); | |
flc(); | |
#end | |
} | |
static function fl() { | |
var a = new FastList<Int>(); | |
for (i in 0...count) | |
a.add(i); | |
var iter = new FastListFI<Int>(a); | |
var sum = 0; | |
Benchmark.measure('FastList chunked', iter.loop( sum += i, i, 1000)); | |
var fl:Iterable<Int> = a; | |
Benchmark.measure('FastList indirect', for (i in fl) sum += i); | |
Benchmark.measure('FastList direct', for (i in a) sum += i); | |
} | |
static function flc() { | |
var a = new FL<Int>(); | |
for (i in 0...count) | |
a.add(i); | |
var iter = new FLFI<Int>(a); | |
var sum = 0; | |
Benchmark.measure('FL chunked', iter.loop( sum += i, i, 1000)); | |
var fl:Iterable<Int> = a; | |
Benchmark.measure('FL indirect', for (i in fl) sum += i); | |
Benchmark.measure('FL direct', for (i in a) sum += i); | |
} | |
static function arr() { | |
var a = []; | |
for (i in 0...count) | |
a.push(i); | |
var iter = new ArrayFI<Int>(a); | |
var sum = 0; | |
Benchmark.measure('Array chunked', iter.loop( sum += i, i, 1000)); | |
var fl:Iterable<Int> = a; | |
Benchmark.measure('Array indirect', for (i in fl) sum += i); | |
Benchmark.measure('Array direct', for (i in a) sum += i); | |
} | |
} | |
import haxe.rtti.Generic; | |
class ArrayFI<T> implements ChunkIterator<T>, implements Generic { | |
var target:Array<T>; | |
public function new(target) { | |
this.target = target; | |
} | |
public function ChunkIter():ChunkIterator<T> { | |
return this; | |
} | |
public function next(buf:ChunkIterBuffer<T>, count:Int, offset:Int):Int { | |
if (count + offset > target.length) | |
count = target.length - offset; | |
var dst = 0; | |
for (src in offset...offset+count) | |
buf[dst++] = target[src]; | |
return count; | |
} | |
} | |
class FastListFI<T> implements ChunkIterator<T>, implements Generic { | |
var target:FastCell<T>; | |
public function new(target:FastList<T>) { | |
this.target = target.head; | |
} | |
public function ChunkIter():ChunkIterator<T> { | |
return this; | |
} | |
public function next(buf:ChunkIterBuffer<T>, count:Int, offset:Int):Int { | |
for (i in 0...count) { | |
if (target == null) return i; | |
buf[i] = target.elt; | |
target = target.next; | |
} | |
return count; | |
} | |
} | |
class FLFI<T> implements ChunkIterator<T>, implements Generic { | |
var target:FC<T>; | |
public function new(target:FL<T>) { | |
this.target = target.head; | |
} | |
public function ChunkIter():ChunkIterator<T> { | |
return this; | |
} | |
public function next(buf:ChunkIterBuffer<T>, count:Int, offset:Int):Int { | |
for (i in 0...count) { | |
if (target == null) return i; | |
buf[i] = target.elt; | |
target = target.next; | |
} | |
return count; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment