package
{
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.utils.*;
/**
* An app to test the functional programming methods of Array and Vector
* @author Jackson Dunstan
*/
public class FunctionalMethodsTest extends Sprite
{
private var logger:TextField = new TextField();
private function log(msg:*): void { logger.appendText(msg + "\n"); }
public function FunctionalMethodsTest()
{
logger.autoSize = TextFieldAutoSize.LEFT;
logger.defaultTextFormat = new TextFormat("_sans", 6);
addChild(logger);
addEventListener(Event.ENTER_FRAME, testVector);
}
private function testVector(ev:Event): void
{
removeEventListener(Event.ENTER_FRAME, testVector);
var beforeTime:int;
var afterTime:int;
var i:int;
var j:int;
const SIZE:int = 7000;
var v:Vector.<int> = new Vector.<int>(SIZE);
var v2:Vector.<int>;
var result:Boolean;
const REPS:int = 7000;
for (i = 0; i < SIZE; ++i)
{
v[i] = i;
}
log("Vector:");
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
v.every(vectorReturnTrue);
}
afterTime = getTimer();
log("\tevery method (all): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
for (j = 0; j < SIZE; ++j)
{
if (!vectorReturnTrue(v[j], j, v))
{
break;
}
}
}
afterTime = getTimer();
log("\tevery manual (all): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
for (j = 0; j < SIZE; ++j)
{
// vectorReturnTrue always passes
}
}
afterTime = getTimer();
log("\tevery inline (all): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
v.every(vectorReturnFalse);
}
afterTime = getTimer();
log("\tevery method (none): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
for (j = 0; j < SIZE; ++j)
{
if (!vectorReturnFalse(v[j], j, v))
{
break;
}
}
}
afterTime = getTimer();
log("\tevery manual (none): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
for (j = 0; j < SIZE; ++j)
{
// vectorReturnFalse always fails
break;
}
}
afterTime = getTimer();
log("\tevery inline (none): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
v.filter(vectorReturnTrue);
}
afterTime = getTimer();
log("\tfilter method (all): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
v2 = new Vector.<int>();
for (j = 0; j < SIZE; ++j)
{
if (vectorReturnTrue(v[j], j, v))
{
v2.push(v[j]);
}
}
}
afterTime = getTimer();
log("\tfilter manual (all): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
v2 = new Vector.<int>();
for (j = 0; j < SIZE; ++j)
{
// vectorReturnTrue always passes
v2.push(v[j]);
}
}
afterTime = getTimer();
log("\tfilter inline (all): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
v.filter(vectorReturnFalse);
}
afterTime = getTimer();
log("\tfilter method (none): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
v2 = new Vector.<int>();
for (j = 0; j < SIZE; ++j)
{
if (vectorReturnFalse(v[j], j, v))
{
v2.push(v[j]);
}
}
}
afterTime = getTimer();
log("\tfilter manual (none): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
v2 = new Vector.<int>();
for (j = 0; j < SIZE; ++j)
{
// vectorReturnFalse always fails
break;
}
}
afterTime = getTimer();
log("\tfilter inline (none): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
v.forEach(vectorReturnVoid);
}
afterTime = getTimer();
log("\tforEach method: " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
for (j = 0; j < SIZE; ++j)
{
vectorReturnVoid(v[j], j, v);
}
}
afterTime = getTimer();
log("\tforEach manual: " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
for (j = 0; j < SIZE; ++j)
{
// vectorReturnVoid does nothing
}
}
afterTime = getTimer();
log("\tforEach inline: " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
v.map(vectorReturnOne);
}
afterTime = getTimer();
log("\tmap method: " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
v2 = new Vector.<int>(SIZE);
for (j = 0; j < SIZE; ++j)
{
v2[j] = vectorReturnOne(v[j], j, v);
}
}
afterTime = getTimer();
log("\tmap manual: " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
v2 = new Vector.<int>(SIZE);
for (j = 0; j < SIZE; ++j)
{
// vectorReturnOne always returns 1
v2[j] = 1;
}
}
afterTime = getTimer();
log("\tmap inline: " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
v.some(vectorReturnFalse);
}
afterTime = getTimer();
log("\tsome method (all): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
result = false;
for (j = 0; j < SIZE; ++j)
{
if (vectorReturnFalse(v[j], j, v))
{
result = true;
break;
}
}
}
afterTime = getTimer();
log("\tsome manual (all): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
result = false;
for (j = 0; j < SIZE; ++j)
{
// vectorReturnFalse always fails
}
}
afterTime = getTimer();
log("\tsome inline (all): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
v.some(vectorReturnTrue);
}
afterTime = getTimer();
log("\tsome method (none): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
result = false;
for (j = 0; j < SIZE; ++j)
{
if (vectorReturnTrue(v[j], j, v))
{
result = true;
break;
}
}
}
afterTime = getTimer();
log("\tsome manual (none): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
result = false;
for (j = 0; j < SIZE; ++j)
{
// vectorReturnTrue always passes
result = true;
break;
}
}
afterTime = getTimer();
log("\tsome inline (none): " + (afterTime-beforeTime));
addEventListener(Event.ENTER_FRAME, testArray);
}
private function testArray(ev:Event): void
{
removeEventListener(Event.ENTER_FRAME, testArray);
var beforeTime:int;
var afterTime:int;
var i:int;
var j:int;
const SIZE:int = 7000;
var a:Array = new Array(SIZE);
var a2:Array;
var result:Boolean;
const REPS:int = 7000;
for (i = 0; i < SIZE; ++i)
{
a[i] = i;
}
log("Array:");
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
a.every(arrayReturnTrue);
}
afterTime = getTimer();
log("\tevery method (all): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
for (j = 0; j < SIZE; ++j)
{
if (!arrayReturnTrue(a[j], j, a))
{
break;
}
}
}
afterTime = getTimer();
log("\tevery manual (all): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
for (j = 0; j < SIZE; ++j)
{
// arrayReturnTrue always passes
}
}
afterTime = getTimer();
log("\tevery inline (all): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
a.every(arrayReturnFalse);
}
afterTime = getTimer();
log("\tevery method (none): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
for (j = 0; j < SIZE; ++j)
{
if (!arrayReturnFalse(a[j], j, a))
{
break;
}
}
}
afterTime = getTimer();
log("\tevery manual (none): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
for (j = 0; j < SIZE; ++j)
{
// arrayReturnFalse always fails
break;
}
}
afterTime = getTimer();
log("\tevery inline (none): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
a.filter(arrayReturnTrue);
}
afterTime = getTimer();
log("\tfilter method (all): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
a2 = new Array();
for (j = 0; j < SIZE; ++j)
{
if (arrayReturnTrue(a[j], j, a))
{
a2.push(a[j]);
}
}
}
afterTime = getTimer();
log("\tfilter manual (all): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
a2 = new Array();
for (j = 0; j < SIZE; ++j)
{
// arrayReturnTrue always passes
a2.push(a[j]);
}
}
afterTime = getTimer();
log("\tfilter inline (all): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
a.filter(arrayReturnFalse);
}
afterTime = getTimer();
log("\tfilter method (none): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
a2 = new Array();
for (j = 0; j < SIZE; ++j)
{
if (arrayReturnFalse(a[j], j, a))
{
a2.push(a[j]);
}
}
}
afterTime = getTimer();
log("\tfilter manual (none): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
a2 = new Array();
for (j = 0; j < SIZE; ++j)
{
// arrayReturnFalse always fails
break;
}
}
afterTime = getTimer();
log("\tfilter inline (none): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
a.forEach(arrayReturnVoid);
}
afterTime = getTimer();
log("\tforEach method: " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
for (j = 0; j < SIZE; ++j)
{
arrayReturnVoid(a[j], j, a);
}
}
afterTime = getTimer();
log("\tforEach manual: " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
for (j = 0; j < SIZE; ++j)
{
// arrayReturnVoid does nothing
}
}
afterTime = getTimer();
log("\tforEach inline: " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
a.map(arrayReturnOne);
}
afterTime = getTimer();
log("\tmap method: " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
a2 = new Array(SIZE);
for (j = 0; j < SIZE; ++j)
{
a2[j] = arrayReturnOne(a[j], j, a);
}
}
afterTime = getTimer();
log("\tmap manual: " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
a2 = new Array(SIZE);
for (j = 0; j < SIZE; ++j)
{
// arrayReturnOne always returns 1
a2[j] = 1;
}
}
afterTime = getTimer();
log("\tmap inline: " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
a.some(arrayReturnFalse);
}
afterTime = getTimer();
log("\tsome method (all): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
result = false;
for (j = 0; j < SIZE; ++j)
{
if (arrayReturnFalse(a[j], j, a))
{
result = true;
break;
}
}
}
afterTime = getTimer();
log("\tsome manual (all): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
result = false;
for (j = 0; j < SIZE; ++j)
{
// arrayReturnFalse always fails
}
}
afterTime = getTimer();
log("\tsome inline (all): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
a.some(arrayReturnTrue);
}
afterTime = getTimer();
log("\tsome method (none): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
result = false;
for (j = 0; j < SIZE; ++j)
{
if (arrayReturnTrue(a[j], j, a))
{
result = true;
break;
}
}
}
afterTime = getTimer();
log("\tsome manual (none): " + (afterTime-beforeTime));
beforeTime = getTimer();
for (i = 0; i < REPS; ++i)
{
result = false;
for (j = 0; j < SIZE; ++j)
{
// arrayReturnTrue always passes
result = true;
break;
}
}
afterTime = getTimer();
log("\tsome inline (none): " + (afterTime-beforeTime));
}
private function vectorReturnTrue(val:int, index:int, vec:Vector.<int>): Boolean
{
return true;
}
private function vectorReturnFalse(val:int, index:int, vec:Vector.<int>): Boolean
{
return false;
}
private function vectorReturnVoid(val:int, index:int, vec:Vector.<int>): void
{
}
private function vectorReturnOne(val:int, index:int, vec:Vector.<int>): int
{
return 1;
}
private function arrayReturnTrue(val:int, index:int, arr:Array): Boolean
{
return true;
}
private function arrayReturnFalse(val:int, index:int, arr:Array): Boolean
{
return false;
}
private function arrayReturnVoid(val:int, index:int, arr:Array): void
{
}
private function arrayReturnOne(val:int, index:int, arr:Array): int
{
return 1;
}
}
}