Skip to content

Instantly share code, notes, and snippets.

@thomasuster
Created October 9, 2012 18:54
Show Gist options
  • Save thomasuster/3860696 to your computer and use it in GitHub Desktop.
Save thomasuster/3860696 to your computer and use it in GitHub Desktop.
Me playing around with different ways to generate a grid of arbitrary size using techniques described in http://alecmce.com/as3/fast-2d-arrays
package
{
import flash.display.Sprite;
import flash.utils.getTimer;
public class MakeGrid extends Sprite
{
private const numCols:uint = 500;
private const numRows:uint = 500;
/*
Output:
vectorStrategy : 56
vectorStrategyWithIntsInsteadOfBools : 44
objectWithMathHash : 42
objectWithMathHashAndOuterLoopMultiplication : 37
objectWithBitwiseHash : 40
objectWithBitwiseHashWithLoopTweak : 53
objectWithBitwiseHashWithLoopTweakAnd0ForBoolean : 46
objectWithBitwiseHashWithLoopTweakAnd0ForBooleanAndDeclareOutter : 43
objectWithMathHashTweakedArray : 26
objectWithMathHashTweakedVector : 20
objectWithMathHashTweakedVectorAndBitwise : 17
*/
public function MakeGrid()
{
//They're probably problems running these consecutivly :)
// testMethod("objectWithStringHash", objectWithStringHash); //slow!
testMethod("vectorStrategy", vectorStrategy);
testMethod("vectorStrategyWithIntsInsteadOfBools", vectorStrategyWithIntsInsteadOfBools);
testMethod("objectWithMathHash", objectWithMathHash);
testMethod("objectWithMathHashAndOuterLoopMultiplication", objectWithMathHashAndOuterLoopMultiplication);
testMethod("objectWithBitwiseHash", objectWithBitwiseHash);
testMethod("objectWithBitwiseHashWithLoopTweak", objectWithBitwiseHashWithLoopTweak);
testMethod("objectWithBitwiseHashWithLoopTweakAnd0ForBoolean", objectWithBitwiseHashWithLoopTweakAnd0ForBoolean);
testMethod("objectWithBitwiseHashWithLoopTweakAnd0ForBooleanAndDeclareOutter", objectWithBitwiseHashWithLoopTweakAnd0ForBooleanAndDeclareOutter);
testMethod("objectWithMathHashTweakedArray", objectWithMathHashTweakedArray);
testMethod("objectWithMathHashTweakedVector", objectWithMathHashTweakedVector);
testMethod("objectWithMathHashTweakedVectorAndBitwise", objectWithMathHashTweakedVectorAndBitwise);
}
private function testMethod(description:String, func:Function):void
{
var startTime:uint = getTimer();
func();
var endTime:uint = getTimer();
trace(description + " : " + String(endTime-startTime));
}
private function objectWithStringHash():void
{
var grid:Object = {};
for (var c:uint = 0; c < numCols; c++)
{
for (var r:uint = 0; r < numRows; r++)
{
grid[String(r) + " " + String(c)] = false;
}
}
}
private function objectWithMathHash():void
{
var grid:Object = {};
for (var c:uint = 0; c < numCols; c++)
{
for (var r:uint = 0; r < numRows; r++)
{
grid[r*numRows+c] = false;
}
}
}
private function objectWithMathHashAndOuterLoopMultiplication():void
{
var grid:Object = {};
for (var c:uint = 0; c < numCols; c++)
{
var major:Number = c*numCols;
for (var r:uint = 0; r < numRows; r++)
{
grid[major+r] = false;
}
}
}
private function vectorStrategy():void
{
var grid:Vector.<Vector.<Boolean>> = new Vector.<Vector.<Boolean>>();
for (var c:uint = 0; c < numCols; c++)
{
var column:Vector.<Boolean> = new Vector.<Boolean>();
grid.push(column);
for (var r:uint = 0; r < numRows; r++)
{
column.push(false);
}
}
}
private function vectorStrategyWithIntsInsteadOfBools():void
{
var grid:Vector.<Vector.<int>> = new Vector.<Vector.<int>>();
for (var c:uint = 0; c < numCols; c++)
{
var column:Vector.<int> = new Vector.<int>();
grid.push(column);
for (var r:uint = 0; r < numRows; r++)
{
column.push(0);
}
}
}
private function objectWithBitwiseHash():void
{
var x:Number = numCols;
var shift:uint = Math.ceil(Math.log(x)/Math.log(2));
var grid:Object = {};
for (var c:uint = 0; c < numCols; c++)
{
for (var r:uint = 0; r < numRows; r++)
{
grid[r<<shift|c] = false;
}
}
}
private function objectWithBitwiseHashWithLoopTweak():void
{
var x:Number = numCols;
var shift:uint = Math.ceil(Math.log(x)/Math.log(2));
var grid:Object = {};
var c:uint = numCols;
while(--c)
{
var r:uint = numRows;
while(--r)
{
grid[r<<shift|c] = false;
}
}
}
private function objectWithBitwiseHashWithLoopTweakAnd0ForBoolean():void
{
var x:Number = numCols;
var shift:uint = Math.ceil(Math.log(x)/Math.log(2));
var grid:Object = {};
var c:uint = numCols;
while(--c)
{
var r:uint = numRows;
while(--r)
{
grid[r<<shift|c] = 0;
}
}
}
//no difference, variables function scoped
private function objectWithBitwiseHashWithLoopTweakAnd0ForBooleanAndDeclareOutter():void
{
var x:Number = numCols;
var shift:uint = Math.ceil(Math.log(x)/Math.log(2));
var grid:Object = {};
var c:uint = numCols;
var r:uint;
while(--c)
{
r = numRows;
while(--r)
{
grid[r<<shift|c] = 0;
}
}
}
private function objectWithMathHashTweakedArray():void
{
var grid:Array = new Array(numCols * numRows);
for (var c:uint = 0; c < numCols; c++)
{
var major:int = c*numRows;
for (var r:uint = 0; r < numRows; r++)
{
grid[major+r] = false;
}
}
}
private function objectWithMathHashTweakedVector():void
{
var grid:Vector.<uint> = new Vector.<uint>(numCols * numRows, true);
for (var c:uint = 0; c < numCols; c++)
{
var major:int = c*numRows;
for (var r:uint = 0; r < numRows; r++)
{
grid[major+r] = 0;
}
}
}
private function objectWithMathHashTweakedVectorAndBitwise():void
{
var x:Number = numCols;
var shift:uint = Math.ceil(Math.log(x)/Math.log(2));
var grid:Vector.<uint> = new Vector.<uint>(numRows<<shift, true);
for (var c:uint = 0; c < numCols; c++)
{
for (var r:uint = 0; r < numRows; r++)
{
grid[r<<shift|c] = 0;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment