#### Archived

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

# speed,speed

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

## Recommended Posts

can you help me about for next loop? do you know any loop is faster than for next? thanks for your helps...

##### Share on other sites
Loops are pretty speedy in general, that's not what is going to make a difference in your app.

The content of the loop will.

loop command speed itself i think.

Borland's for/next loop translates pretty lean in asm.

Hope this helps

Gunner.

Edited by - Gunner on August 29, 2000 3:05:56 PM

##### Share on other sites
As Gunner said, the loop construct itself is not really an issue when it comes to speed. Unless you are doing a very complicated end-of-loop condition.

The contents of the loop have the greatest effect on the loop execution speed.

• Precalculate as many values as possible before the loop and store them in variables. Use the variables inside the loop.
• Unroll loops. The font renderer in the original Quake source is a good example of this. It went something like this...

for (y = 0; y < 7; y++){  (buf++)* = (chardata++)*;  (buf++)* = (chardata++)*;  (buf++)* = (chardata++)*;  (buf++)* = (chardata++)*;  (buf++)* = (chardata++)*;  (buf++)* = (chardata++)*;  (buf++)* = (chardata++)*;  (buf++)* = (chardata++)*;  buf += screenWidth - 8;}

That may not be exactly right, but you should get the idea. Unrolling a loop eliminates a good percentage of the test for the end-of-loop condition.

Steve 'Sly' Williams
Tools Developer
Krome Studios

##### Share on other sites
It looked to me that Holy mentioned "for next" loops. These are from basic compiler. If you want something optimized - give up on that compiler, that's the first thing to do ;-)

Edited by - Lifepower on August 30, 2000 9:58:12 AM

##### Share on other sites

hi,

i want to change pixels of directdrawsurface , so
i use a for next loop ,but the framerate is not as fast as i wanted.

my code;

surfacedesc.dwsize:=sizeof(tddsurfacedesc);
dxdraw1.surface.isurface4.lock(nil,surfacedesc,ddlock_wait,0);

for y:=0 to 639 do
for x:=0 to 479 do
pword(integer(surfacedesc.lpsurface)+x*2+y*surfacedesc.lpitch)^:=color;

dxdraw1surface.isurface4.unlock(@surfacedesc);

color paremeter comes from 2X2 array that is using an array as 2x2..
When i use arrays for my color ,the framerate is too low...
I do not use of any round,trunc commands in the arrays...
But it is too slow...

##### Share on other sites
hi

the standard DXDraw functions for pixel manipulation are MUCH too slow. Download PixelCore from turbo.gamedev.net and use this unit to put pixels on a directdraw surface. The unit is written in pure assembler and it''s very fast.

cya,
tcz

##### Share on other sites
From what I''ve seen you actually put the >same< colour across the entire surface??

You can do that LOTS faster then with those double ForLoops, there is some or other DirectX surface clear to a particular color.

>If< for some reason you want to maintain those Loop-structures, then the following could help:

-Use PixelCore. (Even if I advertise my own product... )

-You seem to use 16bit colour? In that case you can do 2 pixels at once like so:

color,col : integer;

col:=color shl 16 or color;
for y:=0 to 479 do
for x:=0 to 319 do
pword(integer(surfacedesc.lpsurface
+x shl 2+y*surfacedesc.lpitch)^:=col;

or with PixelCore:
col:=color shl 16 or color;
for y:=0 to 479 do for x:=0 to 319 do
PutPixel32(x shl 1, y, col);

Something like that should double the speed.

-Unrolled loops, these were mentioned before; you could implement them as follows:

for y:=0 to 479 do for x:=0 to 79 do begin
Pixel(x shl 3,y,color);
Pixel(x shl 3+1,y,color);
Pixel(x shl 3+2,y,color);
..
..
Pixel(x shl 3+7,y,color);
end;

this basically unrolled the loop 8 times. Though in >this< case the benefits are not necessarily tremendous.

-Precalculation, this too was mentioned before, but you could do it like this:

yy : array[0..479] of integer;

for x:=0 to 479 do yy[x]=x*surface.lpitch; //pre-initialized!
.. //later
for y:=0 to 479 do
for x:=0 to 639 do
pword(integer(surfacedesc.lpsurface
+x shl 1+yy[y])^:=color;

= and then ofcoz you can mix all those methods and optimizations into one seriously fast version. So go-on. Stun us all.

A-Lore

##### Share on other sites
hi ,

yes,i tried pixelcore library,but it does not seem to be fast
as in my code ,also using lots of putpixel16(x,y,color) does not
change speed of it,cause you see i must change all the pixels of
the screen with the colors that have been calculated and that comes from arrays. I thought that i can use pointers for but
the machine shutdown. if i describe a pointer and then i assign
to it my array ,after that using just one line, assigning dxdraw1.surface pointer to my pointer is not working,so i tried
changing all pixels ,pixel by pixel. This way is very stupid,i know. But how can i change directdrawsurface whole using of arrays or pointers ?

Thanks all of...

##### Share on other sites
Hey Holy!

without a more exact description of how your program works - and perhabs more importantly - what you are trying to achieve, we can''t give you specific help.

If you look at the Plasma examples that come with PixelCore, you''ll see that for all pixels a specific color needs to be calculated (real time) involving lots of array references; all that seems to run quite fine.

What speed is your program running at anyway? Are 20fps enough? Waaaay to little? What you got?

- On the pointer and arrays thing... it probably is best if you don''t point any additional pointers to your arrays, as the array name is in itself just a pointer to the array.

using something like:
for y:=0 to 479 do for x:=0 to 639 do
putpixel16(x, y, TheArray[x,y]);

"should" be fine for most purposes. If you absolutely have to improve on this, you can try the following:
write a new (assembler preferably) putpixel procedure, that takes not (x,y) as position, but just (ofs) [the offset, ie the absolute position] of the pixel; you can then use:

for x:=0 to 307199 do putpixel(x,TheArray[x])

which is basically as fast as you can write a non-assembler fullscreen transform.

GoodLuK
A-Lore

##### Share on other sites
You can also make a radical thing like make a loop basead on PutPixel of PixelCore, without calculating X and Y for each single pixel.
But like lore keeper said, the problem can be other. Check, for example, if you are using doFlip on DXDraw.

1. 1
2. 2
Rutin
21
3. 3
4. 4
5. 5
frob
12

• 17
• 9
• 31
• 16
• 9
• ### Forum Statistics

• Total Topics
632614
• Total Posts
3007440

×