Accessing arrays as pointers, speed bonus?

Started by
2 comments, last by Iron Eye 21 years, 3 months ago
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..."
---
ConPong _//_ Google _//_ Chaos Forge - quick and easy file hosting for developers

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



find your elementat mutedfaith.com.
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
I don't have a signature
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;<br>}<br><br>vs.<br><br>int blah[500];<br>int total;<br>int *current;<br><br>current = blah;<br>for (current = blah; *current < 0; current++)<br> total += *current;<br><br> </i>
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 thingsome_type b = a[50];some_type b = *(a + 50); // This will be slowerchar 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 thischar 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.

This topic is closed to new replies.

Advertisement