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?