Archived

This topic is now archived and is closed to further replies.

Help with some slow code...

This topic is 6080 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[ i ][ 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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
400x400 loop = 16000 iterations PER FRAME.

You are doing index look ups... into the video buffer. which equals large number of multiply''s etc.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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(%)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites