Legacy GM Test FunctionA() against FunctionB()

Simon Gust

Member
Might as well make it a thread if that is legal.
What I want here is for people to take similar functions or whole methods and compare their speed.

Like distance_to_object() vs distance_to_point() vs point_distance().
While the functions are different and might not work for every situation, it is still interesting to see which one is really faster. I mean, the GM devs could tell us but where would the fun be in that.

To start this off, like I declared in my status update I wanted to know what the fastest way it is to fill a 128x128 sized surface with pixels of various colors. I will go into it later but clear is:
draw_sprite() not so fast (sprite is 1 pixel large)
draw_point_colour() fast (a bit better than draw_point() with draw_set_color())
draw_vertex_colour() very fast (~50% faster)
all three fulfill the purpose of filling the surface with points / pixels.

So, what are your thoughts? Is it faster for you too? Are there even faster ways?
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
The preferred method of filling an image with individual pixels is to use buffer functions and then buffer_set_surface. Forming a pointlist vertex buffer might be fine too, but a bit of an overkill as for drawing dots (other suggested methods also are though).
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
draw_sprite() not so fast (sprite is 1 pixel large)
draw_point_colour() fast (a bit better than draw_point() with draw_set_color())
draw_vertex_colour() very fast (~50% faster)
Interesting... when comparing speed of code, it's usually best to include the code that was used and the exact values that lead to your final conclusions. If you want to have people discuss this and other comparative functions, then I think it's essential to show the code used to do the comparison...
 

Simon Gust

Member
draw_point_colour() method
Code:
for (var i = 0; i < 255; i++) {
for (var j = 0; j < 255; j++) {
    var c = irandom(c_white);
    draw_point_colour(i, j, c);
}}
draw_vertex_colour() method
Code:
draw_primitive_begin(pr_pointlist);
for (var i = 0; i < 255; i++) {
for (var j = 0; j < 255; j++) {
    var c = irandom(c_white);
    draw_vertex_colour(i, j, c, 1);
}}
draw_primitive_end();
I am going to try the buffer_set_surface method for this raw comparision.
Unfortunately it deviates much from the main loop as the function itself can be hefty.
And in my implementation the loop is way shorter, like 3x128 or so.
128x128 only in worst case scenario.
 

Simon Gust

Member
So I tested with buffer_set_surface, writing in a buffer is 20% faster than writing a vertex. But the buffer_set_surface function makes it slower (relative) the less is actually written / drawn.
Making buffer_set_surface about 15% slower at full reqrite / redraw and at a fraction of rewriting / redrawing it gets up to like 800% slower.
Code:
buffer_seek(buff, 0, 0);
for (var i = 0; i < 255; i++) {
for (var j = 0; j < 255; j++) {
    var c = irandom(c_white);
    buffer_write(buff, buffer_u32, c);
}}
buffer_set_surface(buff, surf, 0, 0, 0);
 
Top