Strange execution speed changes with Adruino Mega

I have a strange issue, and I have no clue what is happening. For starters, this project is for controlling 24 pieces of 8x8 led matrixes as one big display (64x24 pixels).

Below are the code to draw few growing circles to center of the screen 50 times.

uint8_t x = 32;
uint8_t y = 12;

clearPixels(matrix, false);
blinkDisplay(matrix);

for (uint8_t i = 1; i < 50; i++)
{
	clearPixels(matrix, false);
	setCircle(matrix, x, y, i, false);
	if (i > 4) setCircle(matrix, x, y, i - 4, false);
	if (i > 11) setCircle(matrix, x, y, i - 11, false);
	if (i > 20) setCircle(matrix, x, y, i - 20, false);
	if (i > 25) setCircle(matrix, x, y, i - 25, false);

	updateDisplay(matrix);
}

This code draws circles fast.

But if I remove line:

if (i > 25) setCircle(matrix, x, y, i - 25, false);

then drawing speed DECREASES big time (drops at least to half). I don’t think the code behind setCircle or other functions are relevant to this question, I’m just curious what in the world could be the reason that execution speed drops significantly when I’m just drawing LESS? I have tested this with two Arduino Mega board, and same is happening on both boards.

This cannot be memory issue, because I have plenty of memory available:

Sketch uses 9,546 bytes (3%) of program storage space. Maximum is 253,952 bytes.
Global variables use 691 bytes (8%) of dynamic memory, leaving 7,501 bytes for local variables. Maximum is 8,192 bytes.

Any ideas?

Update to this issue:

This has something to do with how much code I have in my sketch. I created function called “demo()”, and it contains the code from the first post. I duplicated that same function 30 times, with different names (demo1, demo2, demo3 … demo30), and removed all unnecessary functions that I earlier had. Then I run all those demo functions in main loop.

Sketch uses 5,326 bytes (2%) of program storage space. Maximum is 253,952 bytes.
Global variables use 415 bytes (5%) of dynamic memory, leaving 7,777 bytes for local variables. Maximum is 8,192 bytes.

Result: code runs slow.

Then I removed demo28(), demo29() and demo30() from main loop.

Sketch uses 5,120 bytes (2%) of program storage space. Maximum is 253,952 bytes.
Global variables use 415 bytes (5%) of dynamic memory, leaving 7,777 bytes for local variables. Maximum is 8,192 bytes.

Result: code runs fast!

Can anyone explain what is happening? There are plenty of free memory and storage space left on board.

I uploaded a video to YouTube to demonstrate this. You can see significant speed boost to circle drawing when I comment out function demo28() and upload the sketch to Arduino.

Does the code that you did not post use Strings ?

UKHeliBob:
Does the code that you did not post use Strings ?

No, I don't use any String. Any text that will be drawn to display uses character arrays. I also use PROGMEM to save characters and numbers to program memory.

UKHeliBob:
Does the code that you did not post ...

That was a hint.

wildbill:
That was a hint.

Ok, I attached the code (zipped, as the project is splitted into multiple files). This is heavily stripped down version of the full code, but the problem is still there.

Ledmatrix64x24.zip (7.8 KB)