Jump to content
  • Advertisement

Archived

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

Iron Eye

Accessing arrays as pointers, speed bonus?

This topic is 5766 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

Is there some kind of performace bonus by accessing an array like this:
  
int SomeValues[2] = {1, 2};

somevar = *(SomeValues + 1) ;
  
Versus:
  
int SomeValues[2] = {1, 2};

somevar = SomeValues[1];
  
Other than,say if you were in some sort of external function you wouldnt need to make a copy of the array? ---
-Iron Eye
Cyrus Eye design  _//_  My personal site  _//_  Google

"Games usually keep me from making my own..."

Share this post


Link to post
Share on other sites
Advertisement
Might be. I read somewhere that using pointer arithmetic is faster than going through each element of the array individually. My memory is kind of fuzzy, though.

I don''t have a signature

Share this post


Link to post
Share on other sites
In your sample, it shouldn''t be faster. There are cases where using a pointer is faster though. The two code blocks below both sum up the values in the array blah, until they find a negative number. The one using pointers may be faster. In this case, it''s hard to say. If you''re doing more in your loop with the pointer than just using it once, you''re more likely to see a gain.

Certain compilers will do calculate blah+i*4 every time you access the array. Before 386 instructions, this took a few extra cycles. Using 386 instructions it has a 1 clock penalty to add i*4 to another register during a memory operation. A good compiler will just figure out the address once, and keep using it.

Since nobody compiles pre-386 code anymore, you don''t really have to worry about it if your structure is a smallish power of two size. Otherwise, using a pointer is best.


int blah[500];
int total;

for (int i=0;i<500;i++)
{
if (blah < 0)
break;
total += blah[i];
}

vs.

int blah[500];
int total;
int *current;

current = blah;
for (current = blah; *current < 0; current++)
total += *current;

Share this post


Link to post
Share on other sites
quote:
Original post by Iron Eye
Other than,say if you were in some sort of external function you wouldnt need to make a copy of the array?
You''ll find it very diffucult to copy an array. An array is nothing more than a pointer to a variable of the array''s type, plus that the compiler takes care of allocating and freeing the memory block. With an array, you cannot pass-by-value, in the sense that the value is the array''s content. The value of your array identifier is a pointer.

For which is fastest? Depends on if the compiler can see the pattern or not.

some_type a[100];

// no difference; it''s the exact same thing
some_type b = a[50];
some_type b = *(a + 50);

// This will be slower
char str[] = "24452ACF";
int pos = 0;
while(str[pos]) {
if(str[pos] >= ''1'' && str[pos] <= ''9'') pos += str[pos] - ''0'' + 1;
if(str[pos] >= ''A'' && str[pos] <= ''Z'') pos -= str[pos] - ''A'' + 1;
}

// Than this
char str[] = "24452ACF";
while(*str) {
if(*str >= ''1'' && *str <= ''9'') str += *str - ''0'' + 1;
if(*str >= ''A'' && *str <= ''Z'') str -= *str - ''A'' + 1;
}
But, they both should probably be structured, so that it had less of an inpact.


Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!