PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Any faster code loop suggestions?

Hi There,

I want to know how can I improve this code or are the any faster way of doing this:

Code:
{
// game loop - loop and loop and loop here

for (int i=0; i<MAX_I; i++)
{
for (int j=0; j<MAX_J; j++)
{
draw(i,j);
}
}
}
Umm, I don't think such micro-optimizations are worth the trouble.

But if you can unroll one (or both) of those loops, it should help performance a little.

If MAX_J is a power of 2, this code might perform better (I wouldn't put money on it):

Code:


for(int ij = 0; ij < MAX_I * MAX_J; ++ij)
draw(ij>>LOG2_MAX_J,ij&(MAX_J-1));


It'll perform differently, anyways.
Andy, your code is most likely slower,since the inner loop cointains 2 additional instructions (prior to the function call).

Anyway, in order to optimize the code, you need to tell us, what draw(i,j) is actually doing. For example, if its doing something like
*(BASE_ADDR+i+j*pitch)=color
, then there is good optimization potential.

Tala
The draw is basically the GD alphablt() so

for i;
for j;
alpablt();

Thats basically it. But I have a lot of for loops for each object so I just to know if there are faster routines out there.

Thanks.
http://www.lysator.liu.se/c/duffs-device.html

Can't believe I almost forgot about this beautiful piece of code.
In x86 stuff, I've always seen loops done like this:
Code:
{
// game loop - loop and loop and loop here

for (int i=MAX_I-1; i; i--)
{
for (int j=MAX_J-1; j; j--)
{
draw(i,j);
}
}
}


I've always assumed it was because the check that i and j is non-zero is faster than a < or >, but I don't know for sure - especially with respect to ARM. I guess you'd have to look at the ASM code to see what 'i<MAX_I' is.
It's the case for ARM as well.

For an incrementing count you'd get:

Code:
add r0,r0,#1
cmp r0,#total
bne loop


Whereas a decrementing count you'd get:

Code:
subs r0,r0,#1
bne loop


(And not have to worry about keeping the count anywhere).
If i were denthorq i would try to get rid of
the call overhead instead of worrying to much
about that compare instruction :)
refractor wrote:It's the case for ARM as well.

For an incrementing count you'd get:

Code:
add r0,r0,#1
cmp r0,#total
bne loop


Whereas a decrementing count you'd get:

Code:
subs r0,r0,#1
bne loop


(And not have to worry about keeping the count anywhere).


I have no idea of how to see code generated for ARM(I can see x86 emulator's code in debugger, but not ARM), but if I could, I'm sure i'd see compiler's already made a plenty of such obvious optimization himself.

PS: How big are max_i and max_j? It must be 10-30, are you sure it is the time to worry about several extra instructions?
jaguard wrote:I have no idea of how to see code generated for ARM(I can see x86 emulator's code in debugger, but not ARM), but if I could, I'm sure i'd see compiler's already made a plenty of such obvious optimization himself.

Of course the compiler will apply such optimisations, but only if you give it the right input.
I think the compiler won't optimise a for loop (based on incrementing counters) with decrementing instructions
to kick out the compare instruction.
I have no idea of how to see code generated for ARM(I can see x86 emulator's code in debugger, but not ARM), but if I could, I'm sure i'd see compiler's already made a plenty of such obvious optimization himself.


For a decrementing counter, yes.

For an incrementing counter, no, never. The compiler cannot take something that increments and make it decrement to save doing the compare.

(I concur with xPeterx, in other words).
I have been following this conversation and my feeling is that it is pointless to optimize the loop when the time is obviously being spent in the Draw() function. Why bother? Did you measure the loops as being a bottleneck? (!)
refractor wrote:
For a decrementing counter, yes.

For an incrementing counter, no, never. The compiler cannot take something that increments and make it decrement to save doing the compare.

(I concur with xPeterx, in other words).


That's very strange. Did anyone ever check this?
It's that I know pretty well that Delphi(said to be the one of the worst optimizers, and its true :)) does this thing.
Agreed (with Thierry). With the loop discussion I was responding to fast_rx's post. I wasn't implying that changing the loop construct itself would significantly benefit the code posted at the top of the thread.
That's very strange. Did anyone ever check this?


I should add the proviso to the "for an incrementing counter, no, never". If it's just a loop where the counter variable is never used apart from the count, then it's possible to optimise it. If the variable is used at all (as in the case above), is global, etc, then it can't be optimised in that way.
jaguard":1fv0ggbg]

It's simple:

The compiler can and most probably will do it if you don't pass the loop variable to a function or use it to address memory in some way.

Just imagine what would happen if you write to a memory mapped IO area, maybe to prepare and start a dma transfer or something like that. If the compiler decides to reverse the write accesses you will have a hard time to find the reason why your program crashes.

Otoh if you just use the loop to do something x times, the compiler is free to decide how the loop should look like.
Oh, yes, you right. But he should be able to detect whenever it will be ok to reverse counter and do it, shouldn't he? For example,

Code:
for (i=0; i< 32; i++)
something[i].some = i*i;
I should add the proviso to the "for an incrementing counter, no, never". If it's just a loop where the counter variable is never used apart from the count, then it's possible to optimise it. If the variable is used at all (as in the case above), is global, etc, then it can't be optimised in that way.


Of course a compiler can do such optimizations, even when the loop variable itself is used, which most likely is the case.
The point is, that the compiler have to analize loop carried dependencies to verify if any affine transformation is valid. In order to do so, it has to determine all side effects. So with a function call within the loop you are out of luck, but it is very well possible if all side effects can be determined.

For instance if no variable from the right hand side appears on the left hand side of an assignement, the loop interations are considered fully permutable. Thus jaguards example is fully permutable, in particular any affine transformation will work.

A good example of a loop carried dependence is the loop:

for (i=0; i<X; i++) a[i]=a[i+1];

Obviously you have a dependence from iteration i->i+1 and the loop cannot be transformed.

Tala
Thus jaguards example is fully permutable, in particular any affine transformation will work.


Imagine I have "something[i]" mapped into a hardware device that's expecting things to be written in ascending order (as torus said, really). Oops, the compiler just changed the behaviour of the program and broke it.

I can see what you're saying for the rest of it, but jaguard's case (and anything that's accessing memory using the counter) should not be "optimised" like that.
I can see what you're saying for the rest of it, but jaguard's case (and anything that's accessing memory using the counter) should not be "optimised" like that.


It is the other way around. A Compiler is allowed to do such optimizations, since according to the language semantic such transformations are allowed.
If you have some underlying semantic (ie. some hardware semantic, which does not behave like memory and can change its state according to the access order) then you will have to switch off such optimizations.

Tala
Tala wrote:It is the other way around. A Compiler is allowed to do such optimizations, since according to the language semantic such transformations are allowed.
If you have some underlying semantic (ie. some hardware semantic, which does not behave like memory and can change its state according to the access order) then you will have to switch off such optimizations.

Although it's true what you're saying i have never seen any compiler which breaks the intention of the code in such a hard way.

For example:
Code:

for (int i = 0; i < YMAX; i++)
for (int j = 0; j < XMAX; j++)
draw(j,i);

results in:
Code:

xor edi,edi
@loop_i:
xor esi,esi
@loop_j:
push esi
push edi
call draw
add esp,8
inc esi
cmp esi,XMAX
jl @loop_j
inc edi
cmp edi,YMAX
jl @loop_i


(Compiled with the VS compiler all optimisations turned on)

But there are cases where the compiler can apply more intelligent optimisations:

Code:

for (int i = 0; i < YMAX; i++)
for (int j = 0; j < XMAX; j++)
{
int offset = i*XMAX+j;
printf("%i\n", offset);
}

If XMAX and YMAX are constant this loop will result in a different piece of asm code,
but can you tell me any compiler which will make this piece of code count backwards?
wow asm veterans :-) thanks for the tips.

i figure out a way to optimize my code, not so efficient but it works. i finally have time to continue my 'xfighter project' and this post is pertaining to optimizing it.

thiery: i agree that the issue is on drawing however i have several loops which i believe can be optimize to cover up with the draw - which cant be avoided ;-)

i dont blame the 'draw' method, maybe my image, but i dont want to get rid of it LOL
If MAX_I, MAX_J are not really big you can always write:

draw(1,1);
draw(1,2);
draw(1,3);
draw(1,4);
draw(1,5);
draw(1,6);
...
draw(2,1);
draw(2,2);
...

Just do it when your program is nearly finished because it looks awful :lol:
Ludens wrote:If MAX_I, MAX_J are not really big you can always write:

draw(1,1);
draw(1,2);
draw(1,3);
draw(1,4);
draw(1,5);
draw(1,6);
...
draw(2,1);
draw(2,2);
...

Just do it when your program is nearly finished because it looks awful :lol:


I do agree ;-)