Last active
February 18, 2025 18:25
-
-
Save tinkerer-red/194ac9b9e0bc180457e64e5de472fafa to your computer and use it in GitHub Desktop.
Custom Matrix Benchmarks
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
Benchmarks = [ | |
new Benchmark("2D Data foreach (full)", [ | |
new TestCase("Array", function(iterations) { | |
var _i=0; repeat(iterations){ | |
arr.foreach(function(_cell, _x, _y){}, false) | |
_i++} | |
}, | |
function(){ | |
width = 32; | |
height = 32; | |
arr = new Array2D(width, height); | |
static __args = {}; | |
__args.arr = arr; | |
__args.value = 123; | |
static __func = method(__args, function(_cell, _x, _y) { | |
arr.set(_x, _y, value) | |
}) | |
arr.foreach(__func, false) | |
}), | |
new TestCase("Grid", function(iterations) { | |
var _i=0; repeat(iterations){ | |
arr.foreach(function(_cell, _x, _y){}, false) | |
_i++} | |
}, | |
function(){ | |
width = 32; | |
height = 32; | |
arr = new Grid2D(width, height); | |
static __args = {}; | |
__args.arr = arr; | |
__args.value = 123; | |
static __func = method(__args, function(_cell, _x, _y) { | |
arr.set(_x, _y, value) | |
}) | |
arr.foreach(__func, false) | |
}), | |
new TestCase("Struct(hash)", function(iterations) { | |
var _i=0; repeat(iterations){ | |
arr.foreach(function(_cell, _x, _y){}, false) | |
_i++} | |
}, | |
function(){ | |
width = 32; | |
height = 32; | |
arr = new Struct2D(width, height); | |
static __args = {}; | |
__args.arr = arr; | |
__args.value = 123; | |
static __func = method(__args, function(_cell, _x, _y) { | |
arr.set(_x, _y, value) | |
}) | |
arr.foreach(__func, false) | |
}), | |
]), | |
new Benchmark("2D Data foreach (full) skip default", [ | |
new TestCase("Array", function(iterations) { | |
var _i=0; repeat(iterations){ | |
arr.foreach(function(_cell, _x, _y){}, true) | |
_i++} | |
}, | |
function(){ | |
width = 32; | |
height = 32; | |
arr = new Array2D(width, height); | |
static __args = {}; | |
__args.arr = arr; | |
__args.value = 123; | |
static __func = method(__args, function(_cell, _x, _y) { | |
arr.set(_x, _y, value) | |
}) | |
arr.foreach(__func, false) | |
}), | |
new TestCase("Grid", function(iterations) { | |
var _i=0; repeat(iterations){ | |
arr.foreach(function(_cell, _x, _y){}, true) | |
_i++} | |
}, | |
function(){ | |
width = 32; | |
height = 32; | |
arr = new Grid2D(width, height); | |
static __args = {}; | |
__args.arr = arr; | |
__args.value = 123; | |
static __func = method(__args, function(_cell, _x, _y) { | |
arr.set(_x, _y, value) | |
}) | |
arr.foreach(__func, false) | |
}), | |
new TestCase("Struct(hash)", function(iterations) { | |
var _i=0; repeat(iterations){ | |
arr.foreach(function(_cell, _x, _y){}, true) | |
_i++} | |
}, | |
function(){ | |
width = 32; | |
height = 32; | |
arr = new Struct2D(width, height); | |
static __args = {}; | |
__args.arr = arr; | |
__args.value = 123; | |
static __func = method(__args, function(_cell, _x, _y) { | |
arr.set(_x, _y, value) | |
}) | |
arr.foreach(__func, false) | |
}), | |
]), | |
new Benchmark("2D Data foreach", [ | |
new TestCase("Array", function(iterations) { | |
var _i=0; repeat(iterations){ | |
arr.foreach(function(_cell, _x, _y){}, false) | |
_i++} | |
}, | |
function(){ | |
width = 32; | |
height = 32; | |
arr = new Array2D(width, height); | |
}), | |
new TestCase("Grid", function(iterations) { | |
var _i=0; repeat(iterations){ | |
arr.foreach(function(_cell, _x, _y){}, false) | |
_i++} | |
}, | |
function(){ | |
width = 32; | |
height = 32; | |
arr = new Grid2D(width, height); | |
}), | |
new TestCase("Struct(hash)", function(iterations) { | |
var _i=0; repeat(iterations){ | |
arr.foreach(function(_cell, _x, _y){}, false) | |
_i++} | |
}, | |
function(){ | |
width = 32; | |
height = 32; | |
arr = new Struct2D(width, height); | |
}), | |
]), | |
new Benchmark("2D Data foreach skip default", [ | |
new TestCase("Array", function(iterations) { | |
var _i=0; repeat(iterations){ | |
arr.foreach(function(_cell, _x, _y){}, true) | |
_i++} | |
}, | |
function(){ | |
width = 32; | |
height = 32; | |
arr = new Array2D(width, height); | |
}), | |
new TestCase("Grid", function(iterations) { | |
var _i=0; repeat(iterations){ | |
arr.foreach(function(_cell, _x, _y){}, true) | |
_i++} | |
}, | |
function(){ | |
width = 32; | |
height = 32; | |
arr = new Grid2D(width, height); | |
}), | |
new TestCase("Struct(hash)", function(iterations) { | |
var _i=0; repeat(iterations){ | |
arr.foreach(function(_cell, _x, _y){}, true) | |
_i++} | |
}, | |
function(){ | |
width = 32; | |
height = 32; | |
arr = new Struct2D(width, height); | |
}), | |
]), | |
new Benchmark("2D Data Setting full", [ | |
new TestCase("Array", function(iterations) { | |
var _i=0; repeat(iterations){ | |
for(var _y=0; _y<height; _y++) for(var _x=0; _x<width; _x++) { | |
arr.set(_x, _y, 123) | |
} | |
_i++} | |
}, | |
function(){ | |
width = 32; | |
height = 32; | |
arr = new Array2D(width, height); | |
static __args = {}; | |
__args.arr = arr; | |
__args.value = 123; | |
static __func = method(__args, function(_cell, _x, _y) { | |
arr.set(_x, _y, value) | |
}) | |
arr.foreach(__func, false) | |
}), | |
new TestCase("Grid", function(iterations) { | |
var _i=0; repeat(iterations){ | |
for(var _y=0; _y<height; _y++) for(var _x=0; _x<width; _x++) { | |
arr.set(_x, _y, 123) | |
} | |
_i++} | |
}, | |
function(){ | |
width = 32; | |
height = 32; | |
arr = new Grid2D(width, height); | |
static __args = {}; | |
__args.arr = arr; | |
__args.value = 123; | |
static __func = method(__args, function(_cell, _x, _y) { | |
arr.set(_x, _y, value) | |
}) | |
arr.foreach(__func, false) | |
}), | |
new TestCase("Struct(hash)", function(iterations) { | |
var _i=0; repeat(iterations){ | |
for(var _y=0; _y<height; _y++) for(var _x=0; _x<width; _x++) { | |
arr.set(_x, _y, 123) | |
} | |
_i++} | |
}, | |
function(){ | |
width = 32; | |
height = 32; | |
arr = new Struct2D(width, height); | |
static __args = {}; | |
__args.arr = arr; | |
__args.value = 123; | |
static __func = method(__args, function(_cell, _x, _y) { | |
arr.set(_x, _y, value) | |
}) | |
arr.foreach(__func, false) | |
}), | |
]), | |
new Benchmark("2D Data Getting full", [ | |
new TestCase("Array", function(iterations) { | |
var _i=0; repeat(iterations){ | |
for(var _y=0; _y<height; _y++) for(var _x=0; _x<width; _x++) { | |
arr.get(_x, _y) | |
} | |
_i++} | |
}, | |
function(){ | |
width = 32; | |
height = 32; | |
arr = new Array2D(width, height); | |
static __args = {}; | |
__args.arr = arr; | |
__args.value = 123; | |
static __func = method(__args, function(_cell, _x, _y) { | |
arr.set(_x, _y, value) | |
}) | |
arr.foreach(__func, false) | |
}), | |
new TestCase("Grid", function(iterations) { | |
var _i=0; repeat(iterations){ | |
for(var _y=0; _y<height; _y++) for(var _x=0; _x<width; _x++) { | |
arr.get(_x, _y) | |
} | |
_i++} | |
}, | |
function(){ | |
width = 32; | |
height = 32; | |
arr = new Grid2D(width, height); | |
static __args = {}; | |
__args.arr = arr; | |
__args.value = 123; | |
static __func = method(__args, function(_cell, _x, _y) { | |
arr.set(_x, _y, value) | |
}) | |
arr.foreach(__func, false) | |
}), | |
new TestCase("Struct(hash)", function(iterations) { | |
var _i=0; repeat(iterations){ | |
for(var _y=0; _y<height; _y++) for(var _x=0; _x<width; _x++) { | |
arr.get(_x, _y) | |
} | |
_i++} | |
}, | |
function(){ | |
width = 32; | |
height = 32; | |
arr = new Struct2D(width, height); | |
static __args = {}; | |
__args.arr = arr; | |
__args.value = 123; | |
static __func = method(__args, function(_cell, _x, _y) { | |
arr.set(_x, _y, value) | |
}) | |
arr.foreach(__func, false) | |
}), | |
]), | |
new Benchmark("2D Data Setting", [ | |
new TestCase("Array", function(iterations) { | |
var _i=0; repeat(iterations){ | |
for(var _y=0; _y<height; _y++) for(var _x=0; _x<width; _x++) { | |
arr.set(_x, _y, 123) | |
} | |
_i++} | |
}, | |
function(){ | |
width = 32; | |
height = 32; | |
arr = new Array2D(width, height); | |
}), | |
new TestCase("Grid", function(iterations) { | |
var _i=0; repeat(iterations){ | |
for(var _y=0; _y<height; _y++) for(var _x=0; _x<width; _x++) { | |
arr.set(_x, _y, 123) | |
} | |
_i++} | |
}, | |
function(){ | |
width = 32; | |
height = 32; | |
arr = new Grid2D(width, height); | |
}), | |
new TestCase("Struct(hash)", function(iterations) { | |
var _i=0; repeat(iterations){ | |
for(var _y=0; _y<height; _y++) for(var _x=0; _x<width; _x++) { | |
arr.set(_x, _y, 123) | |
} | |
_i++} | |
}, | |
function(){ | |
width = 32; | |
height = 32; | |
arr = new Struct2D(width, height); | |
}), | |
]), | |
new Benchmark("2D Data Getting", [ | |
new TestCase("Array", function(iterations) { | |
var _i=0; repeat(iterations){ | |
for(var _y=0; _y<height; _y++) for(var _x=0; _x<width; _x++) { | |
arr.get(_x, _y) | |
} | |
_i++} | |
}, | |
function(){ | |
width = 32; | |
height = 32; | |
arr = new Array2D(width, height); | |
}), | |
new TestCase("Grid", function(iterations) { | |
var _i=0; repeat(iterations){ | |
for(var _y=0; _y<height; _y++) for(var _x=0; _x<width; _x++) { | |
arr.get(_x, _y) | |
} | |
_i++} | |
}, | |
function(){ | |
width = 32; | |
height = 32; | |
arr = new Grid2D(width, height); | |
}), | |
new TestCase("Struct(hash)", function(iterations) { | |
var _i=0; repeat(iterations){ | |
for(var _y=0; _y<height; _y++) for(var _x=0; _x<width; _x++) { | |
arr.get(_x, _y) | |
} | |
_i++} | |
}, | |
function(){ | |
width = 32; | |
height = 32; | |
arr = new Struct2D(width, height); | |
}), | |
]), | |
] |
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
function Array2D(_w, _h, _val=undefined) constructor { | |
width = _w; | |
height = _h; | |
default_value = _val; // only used when resizing to a larger size and initialization of the data | |
data = array_create(_h); | |
//initialize data values | |
for (var _y=0; _y < _h; _y++;) { data[_y] = array_create(_w, _val); }; | |
static get = function(_x, _y) { | |
return data[_y][_x]; | |
} | |
static set = function(_x, _y, _val) { | |
data[_y][_x] = _val; | |
} | |
static foreach = function(_callback, _skip_default=false, _reverse=false) { | |
if (_reverse) { | |
for (var yy = height - 1; yy >= 0; yy--) { | |
var row = data[yy]; | |
for (var xx = width - 1; xx >= 0; xx--) { | |
var cell = row[xx]; | |
if (_skip_default && (cell == default_value)) continue; | |
_callback(cell, xx, yy); | |
} | |
} | |
} | |
else { //read like a book | |
for (var yy = 0; yy < height; yy++) { | |
var row = data[yy]; | |
for (var xx = 0; xx < width; xx++) { | |
var cell = row[xx]; | |
if (_skip_default && (cell == default_value)) continue; | |
_callback(cell, xx, yy); | |
} | |
} | |
} | |
}; | |
} | |
function Grid2D(_w, _h, _val=undefined) constructor { | |
width = _w; | |
height = _h; | |
default_value = _val; // only used when resizing to a larger size and initialization of the data | |
data = ds_grid_create(_w, _h); | |
//initialize data values | |
ds_grid_set_region(data, 0, 0, _w-1, _h-1, _val); | |
static get = function(_x, _y) { | |
return data[# _x, _y]; | |
} | |
static set = function(_x, _y, _val) { | |
data[# _x, _y] = _val; | |
} | |
static foreach = function(_callback, _skip_default=false, _reverse=false) { | |
//i know we could have done this with a single for loop but for performance we did 4 instead | |
if (_reverse) { | |
for (var yy = height - 1; yy >= 0; yy--) { | |
for (var xx = width - 1; xx >= 0; xx--) { | |
var cell = data[# xx, yy]; | |
if (_skip_default && (cell == default_value)) continue; | |
_callback(cell, xx, yy); | |
} | |
} | |
} | |
else { //read like a book | |
for (var yy = 0; yy < height; yy++) { | |
for (var xx = 0; xx < width; xx++) { | |
var cell = data[# xx, yy]; | |
if (_skip_default && (cell == default_value)) continue; | |
_callback(cell, xx, yy); | |
} | |
} | |
} | |
}; | |
} | |
function Struct2D(_w, _h, _val=undefined) constructor { | |
width = _w; | |
height = _h; | |
default_value = _val; // only used when resizing to a larger size and initialization of the data | |
data = {}; | |
static get = function(_x, _y) { | |
var _row = struct_get_from_hash(data, _y) | |
if (_row == undefined) return default_value; | |
return struct_get_from_hash(_row, _x) ?? default_value; | |
} | |
static set = function(_x, _y, _val) { | |
var _row = struct_get_from_hash(data, _y) | |
if (_row == undefined) { | |
_row = {}; | |
struct_set_from_hash(data, _y, _row); | |
} | |
struct_set_from_hash(_row, _x, _val); | |
} | |
static foreach = function(_callback, _skip_default=false, _reverse=false) { | |
if (_reverse) { | |
// Loop from bottom-right to top-left. | |
for (var yy = height - 1; yy >= 0; yy--) { | |
var row = struct_get_from_hash(data, yy); | |
if (_skip_default && (row == undefined)) continue; | |
for (var xx = width - 1; xx >= 0; xx--) { | |
var cell = (row != undefined) ? (struct_get_from_hash(row, xx) ?? default_value) : default_value; | |
if (_skip_default && (cell == default_value)) continue; | |
_callback(cell, xx, yy); | |
} | |
} | |
} | |
else { | |
// Loop from top-left to bottom-right. | |
for (var yy = 0; yy < height; yy++) { | |
var row = struct_get_from_hash(data, yy); | |
if (_skip_default && (row == undefined)) continue; | |
for (var xx = 0; xx < width; xx++) { | |
var cell = (row != undefined) ? (struct_get_from_hash(row, xx) ?? default_value) : default_value; | |
if (_skip_default && (cell == default_value)) continue; | |
_callback(cell, xx, yy); | |
} | |
} | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment