I have a question regarding pointer dereferencing speed. I have this structure:
typedef struct _TD_RECT TD_RECT; struct _TD_RECT { double left; double top; double right; double bottom; };
My question is: which one will be faster and why?
CASE 1:
TD_RECT *pRect; ... for(i = 0; i < m; i++) { if(p[i].x < pRect->left) ... if(p[i].x > pRect->right) ... if(p[i].y < pRect->top) ... if(p[i].y > pRect->bottom) ... }
CASE 2:
TD_RECT *pRect; double left = pRect->left; double top = pRect->top; double right = pRect->right; double bottom = pRect->bottom; ... for(i = 0; i < m; i++) { if(p[i].x < left) ... if(p[i].x > right) ... if(p[i].y < top) ... if(p[i].y > bottom) ... }
So, in case 1, the loop automatically plays the pRect pointer to get comparison values. In case 2, new values ββwere made in the local function space (on the stack), and the values ββwere copied from pRect to local variables. Through the cycle there will be many comparisons.
In my opinion, they will be equally slow because the local variable is also a reference to the memory on the stack, but I'm not sure ...
In addition, it would be better to keep the link p [] at the index or increase p by one element and dereference it directly without the index.
Any ideas? Thanks:)
c ++ c pointers dereference local
oldSkool
source share