Help with some slow code...

Started by
3 comments, last by Absolution 22 years, 12 months ago
I am implementing transparency in an 8-bit game and as a test I tried to just render a transparent black square to the screen every frame. Here is some example code: for(i=100;i<500;i++) { offset=i*mempitch; for(int j=100;j<500;j++) { video_buffer[offset+j]=ttable[0][video_buffer[offset+j]]; } } However, this code runs very slow and it seems to me that it shouldn't. I know it's not optimized, but the performance hit is extreme. I think it has to do with updating the video memory with a new variable index every time through the loop. Does the fact that the new index relies on what was there previously have anything to do with it? Any suggetions would be appreciated. Abs btw. ttable[ j ] represents index j to show through index i - i.e. i is the new transparent colour and j is the background colour. Edited by - Absolution on April 20, 2001 11:16:30 PM
Advertisement
400x400 loop = 16000 iterations PER FRAME.

You are doing index look ups... into the video buffer. which equals large number of multiply''s etc.
Funny thing is though if I change the code to do this :

for(i=100;i<500;i++) {
offset=i*mempitch;
for(int j=100;j<500;j++) {
video_buffer[offset+j]=rand()%255;
UCHAR t=ttable[0][video_buffer[offset+j]];
}
}

I get no noticeable frame-rate decrease. Same number of index look ups. Same operations actually.
quote:Original post by Absolution

Funny thing is though if I change the code to do this :

for(i=100;i<500;i++) {
offset=i*mempitch;
for(int j=100;j<500;j++) {
video_buffer[offset+j]=rand()%255;
UCHAR t=ttable[0][video_buffer[offset+j]];
}
}

I get no noticeable frame-rate decrease. Same number of index look ups. Same operations actually.


It is slower because your are using a division(%)
Original code

for(i=100;i<500;i++)
{
offset=i*mempitch;
for(int j=100;j<500;j++)
{
video_buffer[offset+j]=ttable[0][video_buffer[offset+j]];
}
}


Try something like this:


int *pvb = video_buffer;
int *ptt = &ttable[0][video_buffer[100]]

for(i=100;i<500;i++)
{
pvb += mempitch;
ptt += mempitch;
for(int j=100;j<150;j++) //run code 50 times, not 400
{
*pvb++ = *ptt++; //but do the stuff 8x every loop
*pvb++ = *ptt++; //and use pointer walking
*pvb++ = *ptt++;
*pvb++ = *ptt++;
*pvb++ = *ptt++;
*pvb++ = *ptt++;
*pvb++ = *ptt++;
*pvb++ = *ptt++;
}
}

havent compiled this code or anything so theres probably some buggs in it

This topic is closed to new replies.

Advertisement