#### Archived

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

# 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.

## 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 on other sites
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 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 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 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.

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
633331
• Total Posts
3011390
• ### Who's Online (See full list)

There are no registered users currently online

×