Last active
August 17, 2025 08:15
-
-
Save tinkerer-red/2b2698dd0909e31b5c6c90be31ac9b20 to your computer and use it in GitHub Desktop.
`*_foreach` benchmark
This file contains hidden or 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
new Benchmark("*_foreach comparasons", [ | |
new TestCase("array_foreach()", function(iterations) { | |
array_foreach(array, function(_element, _index){}) | |
}, | |
function(iterations) { | |
array = array_create(iterations); | |
}), | |
new TestCase("array for loop", function(iterations) { | |
for (var _index=0; _index<array_length(array); _index++) { | |
var _element = array[_index]; | |
} | |
}, | |
function(iterations) { | |
array = array_create(iterations); | |
}), | |
new TestCase("array repeat loop", function(iterations) { | |
var _size = array_length(array) | |
var _index = 0; | |
repeat (_size) { | |
var _element = array[_index]; | |
_index++; | |
} | |
}, | |
function(iterations) { | |
array = array_create(iterations); | |
}), | |
new TestCase("string_foreach()", function(iterations) { | |
string_foreach(str, function(_character, _position){}) | |
}, | |
function(iterations) { | |
str = generate_random_string(iterations); | |
}), | |
new TestCase("string for loop", function(iterations) { | |
for (var _position=0; _position<string_length(str); _position++) { | |
var _element = string_char_at(str, _position); | |
} | |
}, | |
function(iterations) { | |
str = generate_random_string(iterations); | |
}), | |
new TestCase("string repeat loop", function(iterations) { | |
var _size = string_length(str) | |
var _position = 0; | |
repeat (_size) { | |
var _element = string_char_at(str, _position); | |
_position++; | |
} | |
}, | |
function(iterations) { | |
str = generate_random_string(iterations); | |
}), | |
new TestCase("struct_foreach()", function(iterations) { | |
struct_foreach(struct, function(_name, _value){}) | |
}, | |
function(iterations) { | |
struct = {}; | |
repeat(iterations) { | |
struct[$ generate_random_string(20)] = 123; | |
} | |
}), | |
new TestCase("struct for loop", function(iterations) { | |
var _names = struct_get_names(struct); | |
for (var _index=0; _index<array_length(_names); _index++) { | |
var _name = _names[_index] | |
var _value = struct[$ _name]; | |
} | |
}, | |
function(iterations) { | |
struct = {}; | |
repeat(iterations) { | |
struct[$ generate_random_string(20)] = 123; | |
} | |
}), | |
new TestCase("struct repeat loop", function(iterations) { | |
var _names = struct_get_names(struct); | |
var _size = array_length(_names) | |
var _index = 0; | |
repeat (_size) { | |
var _name = _names[_index] | |
var _value = struct[$ _name]; | |
_index++; | |
} | |
}, | |
function(iterations) { | |
struct = {}; | |
repeat(iterations) { | |
struct[$ generate_random_string(20)] = 123; | |
} | |
}), | |
new TestCase("struct repeat loop pre-hash", function(iterations) { | |
var _index = 0; | |
repeat (size) { | |
var _hash = hashes[_index] | |
var _value = struct_get_from_hash(struct, _hash); | |
_index++; | |
} | |
}, | |
function(iterations) { | |
struct = {}; | |
size = undefined; | |
hashes = []; | |
repeat(iterations) { | |
struct[$ generate_random_string(20)] = 123; | |
} | |
var _names = struct_get_names(struct); | |
size = array_length(_names) | |
var _index = 0; | |
repeat (size) { | |
var _name = _names[_index]; | |
hashes[_index] = variable_get_hash(_name); | |
_index++; | |
} | |
}), | |
]), |
This file contains hidden or 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 generate_random_string(_length) { | |
var _characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | |
var _char_count = string_length(_characters); | |
var _str = ""; | |
repeat(_length) { | |
var _index = irandom(_char_count - 1) + 1; // strings in GML are 1-indexed | |
_str += string_char_at(_characters, _index); | |
} | |
return _str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment