array index and pointer, which is faster?
- From: Xiaohan <wangxiaohan@xxxxxxxxx>
- Date: Fri, 29 Feb 2008 08:41:23 -0800 (PST)
p is an array and N is a large number. Which of the following two
loops is faster?
Case1:
for(i=0; i<N; i++){
p[i] = i;
}
Case2:
for(i=0; i<N; i++){
*p = i;
p++;
}
It seems that Case 1 needs multiplication/shifting to calculate the
address/index and the second case needs only an ADD operation. But I
tested it, the speed is almost the same. Can anybody tell me why?
Thanks.
For your reference, the assembly code is as follows:
For Case 1
for(i=0; i<N; i++){
0040101B xor ecx,ecx
0040101D lea ecx,[ecx]
p[i] = i;
00401020 mov dword ptr [eax+ecx*4],ecx
00401023 add ecx,1
00401026 cmp ecx,5F5E100h
0040102C jl main+20h (401020h)
//p++;
}
for Case 2, it is:
for(i=0; i<N; i++){
0040101B xor ecx,ecx
0040101D lea ecx,[ecx]
*p = i;
00401020 mov dword ptr [eax],ecx
00401022 add ecx,1
p++;
00401025 add eax,4
00401028 cmp ecx,5F5E100h
0040102E jl main+20h (401020h)
}
.
- Follow-Ups:
- Re: array index and pointer, which is faster?
- From: lawrence . jones
- Re: array index and pointer, which is faster?
- From: Andrey Tarasevich
- Re: array index and pointer, which is faster?
- From: Richard Tobin
- Re: array index and pointer, which is faster?
- Prev by Date: Re: Variable-sized lines of text in linked list
- Next by Date: Re: Is there stack associated when a executing an inline function?
- Previous by thread: dual core and c
- Next by thread: Re: array index and pointer, which is faster?
- Index(es):
Relevant Pages
|