New slides about Go
bearophile
bearophileHUGS at lycos.com
Fri Oct 15 04:43:52 PDT 2010
Walter Bright:
> bearophile wrote:
> > With D sometimes array-based code is faster than pointer-based. With LDC they are usually equally efficient.
>
> ??? This makes no sense.
>
> The (ptr+i*T.sizeof) is an addressing mode on the x86, and comes at ZERO cost.
A little test program:
import std.c.stdio: printf;
size_t add1(size_t[] arr) {
size_t sum = 0;
for (size_t i = 0; i < arr.length; i++)
sum += arr[i];
return sum;
}
size_t add2(size_t[] arr) {
size_t sum = 0;
size_t* p = arr.ptr;
for (size_t i = 0; i < arr.length; i++)
sum += p[i];
return sum;
}
size_t add3(size_t[] arr) {
size_t sum = 0;
size_t* p = arr.ptr;
for (size_t i = 0; i < arr.length; i++)
sum += *p++;
return sum;
}
void main() {
auto arr = new size_t[10_000_000];
foreach (size_t i, ref el; arr)
el = i;
printf("%u\n", add1(arr));
printf("%u\n", add2(arr));
printf("%u\n", add3(arr));
}
---------------------
dmd 2.049
dmd -O -release -inline
_D4test4add1FAkZk comdat
push EAX
xor ECX,ECX
xor EDX,EDX
push EBX
cmp 0Ch[ESP],ECX
je L28
mov 4[ESP],EDX
mov EDX,010h[ESP]
mov EBX,EDX
mov EAX,0Ch[ESP]
mov EDX,4[ESP]
L1E: add ECX,[EDX*4][EBX]
inc EDX
cmp EDX,0Ch[ESP]
jb L1E
L28: pop EBX
mov EAX,ECX
pop ECX
ret 8
_D4test4add2FAkZk comdat
xor ECX,ECX
xor EDX,EDX
cmp 4[ESP],ECX
je L18
LA: mov EAX,8[ESP]
add ECX,[EDX*4][EAX]
inc EDX
cmp EDX,4[ESP]
jb LA
L18: mov EAX,ECX
ret 8
_D4test4add3FAkZk comdat
push EBX
xor EDX,EDX
xor ECX,ECX
cmp 8[ESP],ECX
mov EBX,0Ch[ESP]
je L1D
LF: mov EAX,EBX
add EBX,4
inc ECX
add EDX,[EAX]
cmp ECX,8[ESP]
jb LF
L1D: pop EBX
mov EAX,EDX
ret 8
This has an influence on running time too.
Bye,
bearophile
More information about the Digitalmars-d
mailing list