views:

75

answers:

3

I've read a fair amount of stuff about efficiency of array indices vs. pointers, and how it doesn't really matter unless you're doing something a lot. However, I am doing this a lot.

The code in question has an array of structs. (Two different ones, for two different types actually, but whatever). Since my background is mostly in higher level languages, I defaulted to using a standard particles[i].whatever format. However, I'm not sure if that's a good idea. For a single access, I know it doesn't matter much, but as it stands now, one of my two main functions calls particles[i].something 8 times, and boxes[boxnum].something 4 times per particle, per iteration.

Currently it takes roughly a second to do 5000 particles and 5000 iterations. This means that I'm dealing with these accesses upwards of [including the other function] 200 million times a second. At that frequency, every little bit matters (especially since I'll end up running this code on time on someone else's cluster).

So my question is if it's worth it to do something along the lines of using a pointer to the struct instead of the array access, if gcc will magically do that for me, or if it really doesn't matter.

Thanks ~~Zeb

EDIT: OK, so compiler magic means I shouldn't worry about it. Thanks.

You suggest a profiler, but I can't seem to make gprof tell me any finer-grained information than the time functions take... and I already know that. Is there anything that'll tell me that on a line-by-line basis?

+3  A: 

If you're iterating through the array sequentially, then you might benefit from using a pointer that increments over each loop. There is a slight bit less arithmetic than dereferencing an array. However the compilers are pretty good at optimizing things so you might not see any gain.

Best to run a profiler to see where the problem actually lies. You may be surprised at how far you are from guessing the bottleneck.

Amardeep
I agree about profiling, but there are cases (embedded C on a low end micro) where there are two performance advantages. One is to set up a pointer to the first element of the array and then, assuming the loop is a simple incrementing count, the multiply becomes an add (avoids a function call on very low end processors). The other is to use -> to access the member, to avoid the array access each time once you have the same structure. Depending on the environment, compiler etc this can be significant.
Martin
+1  A: 

I dont think you will see any difference. At the very most you are dropping one integer multiplication per array access, and, in all probabilty gcc will recognise whats happening and optimize it all to pointer arithimatic anyway.

James Anderson
+1  A: 

If you want to improve the running time of your algorithm you should probably try to improve the algorithm per se, not your implementation (unless you have something obviously not optimized).

For example: are there any steps you could skip? Are there any values you could store for the next iteration? This would take more memory, but could save you a lot of time!

Pedro Loureiro