Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance difference in accessing an item in an array vs pointer reference?

Tags:

performance

c

I'm fresh to C - used to scripting languages like, PHP, JS, Ruby etc. Got a query in regard to performance. I know one should not micro optimize too early - however, I'm writing a Ruby C Extension for Google SketchUp where I'm doing lots of 3D calculations so performance is a concern. (And this question is also for learning how C works.)

Often many iterations is done to process all the 3D data so I'm trying to work out what might be faster.

I'm wondering if accessing an array entry many times is faster if I make a pointer reference to that array entry? What would common practice be?

struct FooBar arr[10];
int i;
for ( i = 0; i < 10; i++ ) {
  arr[i].foo = 10;
  arr[i].bar = 20;
  arr[i].biz = 30;
  arr[i].baz = 40;
}

Would this be faster or slower? Why?

struct FooBar arr[10], *item;
int i;
for ( i = 0; i < 10; i++ ) {
  item = &arr[i];
  item->foo = 10;
  item->bar = 20;
  item->biz = 30;
  item->baz = 40;
}

I looked around and found discussions about variables vs pointers - where it was generally said that pointers required extra steps since it had to look up the address, then the value - but in general there wasn't a bit hit.

But what I was wondering was if accessing an array entry in C has much of a performance hit? In Ruby it is faster to make a reference to the entry if you need to access it many time - but that's Ruby...

like image 770
thomthom Avatar asked Sep 03 '26 03:09

thomthom


1 Answers

There's unlikely to be a significant difference. Possibly the emitted code will be identical. This is assuming a vaguely competent compiler, with optimization enabled. You might like to look at the disassembled code, just to get a feel for some of the things a C optimizer gets up to. You may well conclude, "my code is mangled beyond all recognition, there's no point worrying about this kind of thing at this stage", which is a good instinct.

Conceivably the first code could even be faster, if introducing the item pointer were to somehow interfere with any loop unrolling or other optimization that your compiler performs on the first. Or it could be that the optimizer can figure out that arr[i].foo is equal to stack_pointer + sizeof(FooBar) * i, but fail to figure that out once you use the pointer, and end up using an extra register, spilling something else, with performance implications. But I'm speculating wildly on that point: there is usually little to no difference between accessing an array by pointer or by index, my point is just that any difference there is can come for surprising reasons.

like image 90
Steve Jessop Avatar answered Sep 04 '26 23:09

Steve Jessop