Array reverse
bearophile
bearophileHUGS at lycos.com
Fri Nov 23 01:14:19 PST 2012
I've seen a difference in the performance of
std.algorithm.reverse applied on an array compared to home-made
code, so I have written a little test.
Below you see the code, and the asm of the functions, compiled
with DMD 2.060, 32 bit (-O -release -inline).
Feel free to recompile the code yourself to see if I have done
some mistake :-)
------------------------------
import core.stdc.stdio: printf;
import std.algorithm: reverse;
void reverseArr(T)(T[] arr) {
for (auto x = arr.ptr, y = &arr[$ - 1]; x < y; ) {
auto aux = *x;
*x++ = *y;
*y-- = aux;
}
}
void main() {
auto a = [10, 20, 30, 40];
foreach(x; a) printf("%d ", x); printf("\n");
a.reverseArr();
foreach(x; a) printf("%d ", x); printf("\n");
a.reverse();
foreach(x; a) printf("%d ", x); printf("\n");
}
------------------------------
reverseArr:
push EBX
mov ECX,0Ch[ESP]
mov EBX,0Ch[ESP]
push ESI
mov ESI,0Ch[ESP]
lea ESI,-4[ESI*4][ECX]
cmp ECX,ESI
jae L2B
L19: mov EAX,[EBX]
mov EDX,[ESI]
mov [EBX],EDX
add EBX,4
mov [ESI],EAX
add ESI,0FFFFFFFCh
cmp EBX,ESI
jb L19
L2B: pop ESI
pop EBX
ret 8
-------------------
std.algorithm.reverse:
L0: sub ESP,020h
push EBX
push ESI
push EDI
cmp dword ptr 030h[ESP],0
je LE1
L11: cmp dword ptr 030h[ESP],0
mov EAX,030h[ESP]
mov EDX,034h[ESP]
mov 018h[ESP],EAX
mov 01Ch[ESP],EDX
jne L32
mov EAX,022Dh
call near ptr _D3std9algorithm7__arrayZ
L32: mov EAX,030h[ESP]
mov EDX,034h[ESP]
mov 024h[ESP],EDX
mov ECX,030h[ESP]
lea EDX,-1[ECX]
mov 020h[ESP],EAX
cmp EDX,ECX
mov 010h[ESP],EDX
jb L5E
mov EAX,0258h
call near ptr _D3std9algorithm7__arrayZ
L5E: mov ECX,01Ch[ESP]
mov EBX,030h[ESP]
mov EDX,024h[ESP]
lea EBX,-4[EBX*4][EDX]
mov ESI,[ECX]
mov EDX,[EBX]
mov [ECX],EDX
mov ECX,030h[ESP]
cmp ECX,ECX
mov [EBX],ESI
ja L86
cmp ECX,1
jae L90
L86: mov EAX,0173h
call near ptr _D3std9algorithm7__arrayZ
L90: mov EDX,034h[ESP]
mov EDI,010h[ESP]
mov 030h[ESP],EDI
add EDX,4
mov 034h[ESP],EDX
cmp dword ptr 030h[ESP],0
je LE1
mov EBX,030h[ESP]
lea ECX,-1[EBX]
cmp ECX,EBX
mov 014h[ESP],ECX
jbe LC6
mov EAX,01E8h
call near ptr _D3std9algorithm7__arrayZ
LC6: mov ESI,014h[ESP]
mov EDX,034h[ESP]
mov 030h[ESP],ESI
mov 034h[ESP],EDX
cmp dword ptr 030h[ESP],0
jne L11
LE1: pop EDI
pop ESI
pop EBX
add ESP,020h
ret 8
------------------------------
Bye,
bearophile
More information about the Digitalmars-d
mailing list