c - Efficiency between pointer and array(less assembly instructions doesn't take less time) -
this question has answer here:
- efficiency: arrays vs pointers 12 answers
some people said: "any operation can achieved array subscripting can done pointers. pointer version in general faster".
i doubt outcome of above, following test:
in following article, not care compiler optimization. compiler optimization how affect efficiency between pointer , array, please note: efficiency: arrays vs pointers
(visual studio 2010, debug mode, no optimizations)
#include <windows.h> #include <stdio.h> int main() { int a[] = {10,20,30}; int* ap = a; long counter; int start_time, end_time; int index; start_time = gettickcount(); (counter = 1000000000l; counter>0; counter--) { *(ap+1) = 100; } end_time = gettickcount(); printf("10 billion times of *ap = %d\n", end_time-start_time); start_time = gettickcount(); (counter = 1000000000l; counter>0; counter--) { a[1] = 101; } end_time = gettickcount(); printf("10 billion times of a[0] = %d\n", end_time-start_time); return 0; }
the result is:
10 billion times of *ap = 3276 10 billion times of a[0] = 3541
the pointer seems little fast. after compared dis-assemble, fell deeper confusion。
(visual studio 2010, debug mode, no optimizations)
; 17 : *(ap+1) = 100; mov eax, dword ptr _ap$[ebp] mov dword ptr [eax+4], 100 ; 00000064h ; 25 : a[1] = 101; mov dword ptr _a$[ebp+4], 101 ; 00000065h
from assemble output, memory access through pointer takes 2 instructions , array takes 1 instruction.
why array execute less instructions doesn't takes less time pointer?
does associated cpu cache? how can modify test code prove it?
firstly , importantly, c language doesn't have speed. that's attribute introduced implementations of c. example, c doesn't have speed, gcc compiler produces code might vary in speed code produced clang compiler, , they're both produce code out-performs behavior produced cint or ch interpreters. of these c implementations. of them slower others, the speed can't attributed c in anyway!
6.3.2.1 of c standard says:
except when operand of sizeof operator, _alignof operator, or unary & operator, or string literal used initialize array, expression has type ‘‘array of type’’ converted expression type ‘‘pointer type’’ points initial element of array object , not lvalue.
this ought indication both *(ap+1)
, a[1]
in code pointer operations. translation happen during compilation phase in visual studio. hence, shouldn't have impact on runtime.
6.5.2.1 in regards "array subscripting" says:
one of expressions shall have type ‘‘pointer complete object type’’, other expression shall have integer type, , result has type ‘‘type’’. seems indicate array subscript operator pointer operator...
this confirmation ap[1]
indeed pointer operation, postulated earlier. @ runtime, however, array has been translated pointer. performance should identical.
... so, why aren't identical?
what characteristics of os you're using? isn't multitasking, multi-user os? suppose os complete first loop without interruption, interrupt second loop , switch control different process. wouldn't interruption invalidate experiment? how measure frequency , timing of interruptions caused task switching? note differ different oses, , os part of implementation.
what characteristics of cpu you're using? have it's own fast, internal cache machine code? suppose entire first loop, , it's encompassing timing mechanism fit code cache nicely, second loop truncated. wouldn't result in cache miss, , long wait while cpu fetches remainder of code ram? how measure timing of interruptions caused cache misses? note differ different cpus, , cpu part of implementation.
these questions ought raise questions such "does micro-optimisation benchmark solve meaningful or important problem?". success of optimisation differ depending upon size , complexity of problem. find important problem, solve it, profile solution, optimise , profile again. way, can give meaningful information how faster optimised version is. boss happier you, providing don't divulge optimisations relevant implementation, mentioned earlier. i'm you'll find least of worries array dereference vs pointer dereference.
Comments
Post a Comment