Some C book author says that accessing an array value through the pointer is faster than through array indexing. But is this true? Here are two programs that I have written to understand the concept.
main()
{
int arr[100000],*ptr,c,i;
ptr=arr;
for(i=0;i<100000;i++)
arr[i]=i;
for(i=0;i<100000;i++)
c = arr[i];
}
main()
{
int arr[100000],*ptr,c,i;
ptr=arr;
for(i=0;i<100000;i++)
*(ptr+i)=i;
for(i=0;i<100000;i++)
c = *(ptr+i);
}
When I run these two with time ./a.out the second program takes more time. This means the use of pointers takes more time than the array index method.
Could you please explain to me how this works, with these simple programs?
timeprocess monitor we'd all be overpaid statisticians.-g' flag when compiling withgcc.objdump -D a.out > file, for both the programs. Remember to create two different files for each program. Now do vimdiff file1 file2. Compare the difference, however I think there won't be any difference.