K
kevins_office
Guest
I am trying to get faster creation and destruction times out of 2D arrays.
When using normal GML 2D arrays[x , y] they get created with the default GML Real data type, not only does this bloat the memory on large arrays[1000, 1000] it makes it very slow.
I tried making buffers like an array using peek/poke with offset to store and fetch values. This greatly improved creation time from 11,000µs to 300µs. However access time is 5 times slower than using 2D arrays.
So before i toss the baby out with the bath water i want to ask the community if buffers actually are slower to use or if it is my code slowing it down. Here are the scripts...
Is there a faster way to accomplish this? Another method beside buffers?
When using normal GML 2D arrays[x , y] they get created with the default GML Real data type, not only does this bloat the memory on large arrays[1000, 1000] it makes it very slow.
I tried making buffers like an array using peek/poke with offset to store and fetch values. This greatly improved creation time from 11,000µs to 300µs. However access time is 5 times slower than using 2D arrays.
So before i toss the baby out with the bath water i want to ask the community if buffers actually are slower to use or if it is my code slowing it down. Here are the scripts...
Code:
/// @func fast_array_create(w_size, h_size, byte_size)
/// @arg w_size
/// @arg h_size
/// @arg byte_size
// byte_size 1: 0 to 255 (buffer_u8)
// byte_size 2: 0 to 65,535 (buffer_u16)
// byte_size 4: -2,147,483,648 to 2,147,483,647 (buffer_s32)
var wSize = argument[0];
var hSize = argument[1];
var byteSize = argument[2];
var array_id = buffer_create((wSize * hSize * byteSize) + 4, buffer_fixed, byteSize);
buffer_poke(array_id, 0, buffer_u32, wSize);
return array_id;
Code:
/// @func fast_array_destroy(array_id)
/// @arg array_id
buffer_delete(argument[0]);
return;
Code:
/// @func fast_array_set(array_id, cell_x, cell_y, value)
/// @arg array_id
/// @arg cell_x
/// @arg cell_y
/// @arg value
var array_id = argument[0];
var cell_x = argument[1];
var cell_y = argument[2];
var value = argument[3];
var wSize = buffer_peek(array_id, 0, buffer_u32);
var byteSize = buffer_get_alignment(array_id);
var bufferType = buffer_u8;
if (byteSize == 2) bufferType = buffer_u16;
if (byteSize == 4) bufferType = buffer_s32;
var pos = (cell_y * wSize * byteSize) + (cell_x * byteSize) + 4;
buffer_poke(array_id, pos, bufferType, value);
return;
Code:
/// @func fast_array_get(array_id, cell_x, cell_y)
/// @arg array_id
/// @arg cell_x
/// @arg cell_y
var array_id = argument[0];
var cell_x = argument[1];
var cell_y = argument[2];
var wSize = buffer_peek(array_id, 0, buffer_u32);
var byteSize = buffer_get_alignment(array_id);
var bufferType = buffer_u8;
if (byteSize == 2) bufferType = buffer_u16;
if (byteSize == 4) bufferType = buffer_s32;
var pos = (cell_y * wSize * byteSize) + (cell_x * byteSize) + 4;
return buffer_peek(array_id, pos, bufferType);
Last edited by a moderator: