At the AckCon I was running a test of a multiplayer game on various machines. On many machines the FPS was all over the place, in particular not consistent at all (as in let's say jumping between 30 and 90 FPS or equivalent ms of processing every few frames).

I've narrowed down the performance bottlenecks using dtimer(). Turns out, drawing text on screen (for example for names or live rankings) consumes way more performance than all other things in the game combined.

Example test project:
Click to reveal..
Code:
///////////////////////////////
#include <acknex.h>
#include <default.c>
///////////////////////////////

FONT* dummy_fnt = "Courier New#40b";

TEXT* dummy_txt =
{
	string("");
	font = dummy_fnt;
	flags = LIGHT | OUTLINE;
}

TEXT* array_txt[30];

void main()
{
	fps_max = 60;
	video_mode = 9;
	vec_fill(screen_color,64);
	double dMax = 0;
	/*short shortArray[4];
	shortArray[0] = 0xd853;
	shortArray[1] = 0xdf5c;
	shortArray[2] = shortArray[3] = 0;
	STRING* strW = str_createw(shortArray); regular ASCII text is enough*/
	
	int i;
	for(i = 0; i < 30; i++)
	{
		TEXT* txt = txt_create(1,1);
		str_printf((txt.pstring)[0],"String %d at frame 1234",i);
		txt.font = dummy_fnt;
		txt.flags = LIGHT | OUTLINE;
		txt.pos_x = 10;
		txt.pos_y = 60+20*i;
		array_txt[i] = txt;
	}
	
	while(1)
	{
		//draw_text(strW,800,20,COLOR_RED);
		dtimer();
		int i;
		for(i = 0; i < 30; i++)
		{
			if(key_space) draw_obj(array_txt[i]);
			else
			{
				STRING* str = str_printf(NULL,"String %d at frame %d",i,(int)total_frames);
				//draw_text(str,10,60+20*i,COLOR_RED);
				str_cpy((dummy_txt.pstring)[0],str);
				dummy_txt.pos_x = 10;
				dummy_txt.pos_y = 60+20*i;
				vec_set(dummy_txt.blue,COLOR_WHITE);
				draw_obj(dummy_txt);
			}
		}
		double dTime = dtimer();
		if(dTime*0.001 > dMax) dMax = dTime*0.001;
		if(total_frames%120 == 0) dMax = 0;
		draw_text(str_printf(NULL,"Time for drawing 30 strings: %.3fms (max: %.3fms)",(double)(dTime*0.001),dMax),10,20,COLOR_RED);
		
		wait(1);
	}
}



My desktop PC runs on an i7-6700K. It takes around 2.7ms on average to draw 30 strings, with spikes of 10+ms every few seconds.
On my weaker (i7 still) laptop I have an average execution time of 5ms but highly fluctuating betwen 4.9ms and 10ms, with spikes of 13+ms.


Why it is that slow and why are there huge differences in execution time (including but not limited to the spikes), instead of being about the same every frame?
Can this be fixed/ improved?


"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Check out my new game: Pogostuck: Rage With Your Friends