Advertisement

How would you optimise this?

Started by July 30, 2000 04:55 PM
5 comments, last by TheGecko 24 years, 4 months ago
Hey all, Suppose I have this structure:

typedef struct
{
	float u;	//Texture coordinate u
	float v;	//Texture coordinate v

	BOOL Enabled;	//Is this vertex enabled ?

}Vertex;
     
and I want to initialise all the vertices I have (513x513 vertices) such that the Enabled flag is set to false like this:

for(int i=0 ; i < MapSize ; i++)
 for(int j=0 ; j < MapSize ; j++)
   Vertices[(i*MapSize) + j].Enabled = FALSE;
 
Is there anyway to optimise that piece of code to run even faster? It's the big bottleneck in my app and I would appreciate any help in fixing it.Thanx! Edited by - TheGecko on 7/30/00 4:58:13 PM Edited by - TheGecko on 7/30/00 4:58:40 PM
You could do this to speed it up...

int MapSizeSquared = MapSize * MapSize;for(int offset=0; offset < MapSizeSquared; offset++)   Vertices[offset].Enabled = FALSE; 


BTW, You may not have to precompute MapSizedSquared - I can''t remember if it gets compiled that way automatically or not.)


lntakitopi@aol.com - http://www.geocities.com/guanajam
Advertisement
Move that bEnabled flag into its own chunk of memory.. and then memset it.

A structure along the lines of:

typedef struct
{
PVector pVertList; // vertex array
PVector pUVList; // uv vertex array
PBOOL pEnabledList; // enabled vert list
} MapData, *PMapData;

all members of this structure would be allocated to the same number of elements.. then you could clear the enabled flags by:

PMapData pMapData;

memset(&pMapData->pEnabledList, 0, sizeof(BOOL) * NUMBER_OF_VERTICES);

sometimes for performance reasons, it just makes sense to seperate out certain elements of your structures.. especially if you''re getting performance bottlenecks.

also, by changing your BOOL vars to just BYTES, that would be 1/4 the number of bytes you have to memset. i.e.:

PBYTE pEnabledList;

good luck
If you knew that you were going to set u and v later, and that their starting values don''t matter, then it would probably be faster to use ZeroMemory on the whole array, that way u, v, and enabled will all hold 0. I think that will be faster than using two loops and lots of assignment statements, but I''m not sure.

*** Triality ***
*** Triality ***
hey
I dont know if this will be faster, but you could get rid of the lookup all together using pointer arithmetic. Do like shilbert said and precalculate mapSizeSquared then do this

int mapSizeSquared = mapSize * mapSize;

for(int i = 0; i < mapSizeSquared; i++, Vertices++)
*Vertices.Enabled = FALSE;

OR you could write a constructor for the structure that automatically sets Enabled to false. This will most likely be faster because it eliminated the loop altogether AND it eliminates the need for a lookup or pointer arithmetic.

Like:

stuct Vertex{
float u;
float v;
BOOL Enabled;
Vertex(): u(0), v(0), Enabled(FALSE){ }
};

hope that helps,
skitzo_smurf




Edited by - skitzo_smurf on July 30, 2000 6:19:07 PM
"Innocent is just a nice way to say ignorant, and stupidity is ignorance with its clothes off."words of,skitzo_smurf
Thanx guys! That worked fine! If anyone has any other suggestions I''d like to hear them.As for now everything is working a little bit better using just one for loop.
Advertisement
Put your enabled flags in a seperate list and make your enable flag a bit and set 32 of them to zero at a time with one statement.

_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.

This topic is closed to new replies.

Advertisement