Advertisement

Pointer optimisation

Started by February 14, 2002 10:49 AM
2 comments, last by tifkagh 22 years, 8 months ago
I read something recently that stated that pointers are a more efficient way to hold data. Is this purely from the point of view of flexibility or are pointers faster? Cheers, tha tif.
depends on how you use them. Imagine plotting points on the screen (pitch = width), and the screen is stored in an array[640][480].

slow way using arrays
  for(y = 0; y < SCREEN_HEIGHT; y++)    for (x = 0; x < SCREEN_WIDTH; x++)        ScreenArray[x][y] = SomeColor;  


faster way using pointer
  Ptr = (ScreenPixel *)ScreenArray;EndPtr = (ScreenPixel *)((unsigned int)Ptr + (SCREEN_WIDTH*SCREEN_HEIGHT);while((unsigned int)Ptr < (unsigned int)EndPtr)    *Ptr++ = SomeColor;  



using the pointer removes all the multiplication every frame, and it only does 1 addition and 1 comparison.

Just an example.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Advertisement
I think the article in question was recommending that sizable objects should be stored using pointers. Is this correct? If so what size of object would benefit from this?
You can''t store an object with a pointer. A pointer is a number that identifies a spot in your computer''s memory. So the pointer ''points'' to where the data actually is.

Using pointers is fast if you want to pass around a large amount of data. So, if you had a struct that was comprised of 50 ints, whenever you pass that struct into another function you have to copy all 50 of those ints. If you pass a pointer to the struct, all you have to copy is the one pointer (generally about the size of one int). See where this gets you a little speed?

Of course, pointers are good for many other things as well. I suggest picking up a good intro to C/C++ book.

"So crucify the ego, before it''s far too late. To leave behind this place so negative and blind and cynical, and you will come to find that we are all one mind. Capable of all that''s imagined and all conceivable."
- Tool
------------------------------
"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut

This topic is closed to new replies.

Advertisement