Memory access through pointers is said to be more efficient than memory access through an array. I am learning C and the above is stated in K&R. Specifically they say
Any operation that can be achieved by array subscripting can also be done with pointers. The pointer version will in general be faster
I dis-assembled the following code using visual C++.(Mine is a 686 processor. I have disabled all optimizations.)
int a[10], *p = a, temp;
void foo()
{
temp = a[0];
temp = *p;
}
To my surprise I see that memory access through a pointer takes 3 instructions to the two taken by memory access through an array. Below is the corresponding code.
; 5 : temp = a[0];
mov eax, DWORD PTR _a
mov DWORD PTR _temp, eax
; 6 : temp = *p;
mov eax, DWORD PTR _p
mov ecx, DWORD PTR [eax]
mov DWORD PTR _temp, ecx
Please help me understand. What am I missing here??
As pointed out by many answers and comments I had used a compile time constant as the array index thus making it arguably easier for the access through an array. Below is the assembly code with a variable as the index. I now have equal number of instructions for access through pointer and arrays. My broader questions still holds good. The memory access through a pointer is not lending itself as being more efficient.
; 7 : temp = a[i];
mov eax, DWORD PTR _i
mov ecx, DWORD PTR _a[eax*4]
mov DWORD PTR _temp, ecx
; 8 :
; 9 :
; 10 : temp = *p;
mov eax, DWORD PTR _p
mov ecx, DWORD PTR [eax]
mov DWORD PTR _temp, ecx
That may have been true in the past when compilers were relatively stupid beasts. You only need to look at some of the code output by
gccin high optimisation modes to know that it is no longer true. Some of that code is very hard to understand but, once you do, its brilliance is evident.A decent compiler will generate the same code for pointer accesses and array accesses and you should probably not be worrying about that level of performance. The people that write compilers know far more about their target architectures than we mere mortals. Concentrate more on the macro level when optimising your code (algorithm selection and so on) and trust in your tool-makers to do their job.
In fact, I’m surprised the compiler didn’t optimise the entire
line out of existence since
tempis over-written in the very next line with a different value andais in no way markedvolatile.I remember an urban myth from long ago about a benchmark for the latest VAX Fortran compiler (showing my age here) that outperformed its competitors by several orders of magnitude.
Turns out the compiler figured out that the result from the benchmark calculation wasn’t used anywhere so it optimised the entire calculation loop into oblivion. Hence the substantial improvement in run speed.
Update: The reason that optimised code is more efficient in your particular case is because of the way you find the location.
awill be at a fixed location decided at link/load time and the reference to it will be fixed up at the same time. Soa[0]or indeeda[any constant]will be at a fixed location.And
pitself will also be at a fixed location for the same reason. But*p(the contents ofp) is variable and therefore will have an extra lookup involved to find the correct memory location.You’ll probably find that having yet another variable
xset to 0 (notconst) and usinga[x]would also introduce extra calculations.In one of your comments, you state:
My response to that is that you very likely won’t see an efficiency in using pointers. Modern compilers are more than up to the task of figuring out that array operations and pointer operations can be turned into the same underlying machine code.
In fact, without optimisation turned on, pointer code can be less efficient. Consider the following translations:
From that example, you can actually see that the pointer example is longer, and unnecessarily so. It loads
painto%eaxmultiple times without it changing and indeed alternates%eaxbetweenpaand&(a[10]). The default optimisation here is basically none at all.When you switch up to optimisation level 2, the code you get is:
for the array version, and:
for the pointer version.
I’m not going to do an analysis on clock cycles here (since it’s too much work and I’m basically lazy) but I will point out one thing. There’s not a huge difference in the code for both versions in terms of assembler instructions and, given the speeds that modern CPUs actually run at, you won’t notice a difference unless you’re doing billions of these operations. I always tend to prefer writing code for readability and only worrying about performance if it becomes an issue.
As an aside, that statement you reference:
dates back to the earliest versions of K&R, including my ancient 1978 one where functions are still written:
Compilers have come an awfully long way since back then.