Skip to content

Instantly share code, notes, and snippets.

@YellowAfterlife
Created September 9, 2015 19:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YellowAfterlife/6d733315bf0554aba659 to your computer and use it in GitHub Desktop.
Save YellowAfterlife/6d733315bf0554aba659 to your computer and use it in GitHub Desktop.
Comparing various methods of iterating over ds_map
var map = ds_map_create();
var keys = ds_list_create();
var values = ds_list_create();
var num = 10000;
var t, k, v;
for (var i = 0; i < num; i++) {
k = i * 2 + 1;
v = irandom(10);
ds_list_add(keys, k);
ds_list_add(values, v);
map[?k] = v;
}
// a regular situation:
t = current_time;
k = ds_map_find_first(map);
repeat (ds_map_size(map)) {
v = map[?k];
k = ds_map_find_next(map, k);
}
show_debug_message("normal: " + string(current_time - t)); // 721
// a separate list holds the map' keys:
t = current_time;
for (var i = 0; i < num; i++) {
k = keys[|i];
v = map[?k];
}
show_debug_message("keys[]: " + string(current_time - t)); // 9
// ds_map is used for key->index pairs, actual data is stored in lists:
t = current_time;
for (var i = 0; i < num; i++) {
k = keys[|i];
v = values[|i];
}
show_debug_message("keys[]+values[]: " + string(current_time - t)); // 6
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment