1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
| #include <stdio.h>
#include <windows.h>
#define POINT_COUNT 40000000
#define FUNCTION_TO_TEST_COUNT 6
#define TEST_COUNT 20
typedef struct
{
int x;
int y;
}
MY_POINT;
/* Proto des fonctions à tester */
typedef void (__stdcall* FUNCTION_TO_TEST) (MY_POINT *lpPoints, int nPointCount, MY_POINT *lpDelta);
/* Contient le nom d'une fonction à tester et un pointeur sur celle-ci */
typedef struct _FUNCTION_TO_TEST_INFO
{
char name[64];
FUNCTION_TO_TEST function;
int duration;
}
FUNCTION_TO_TEST_INFO;
/* Implémentation candide */
void __stdcall UpdatePointsSimple(MY_POINT *lpPoints, int nPointCount, MY_POINT *lpDelta)
{
int i;
for (i = 0; i < nPointCount; i++)
{
lpPoints[i].x += lpDelta->x;
lpPoints[i].y += lpDelta->y;
}
}
/* Implémentation avec copie de lpDelta (Revient à utiliser restrict) */
void __stdcall UpdatePointsCopyDelta(MY_POINT *lpPoints, int nPointCount, MY_POINT *lpDelta)
{
MY_POINT delta;
int i;
delta = *lpDelta;
for (i = 0; i < nPointCount; i++)
{
lpPoints[i].x += delta.x;
lpPoints[i].y += delta.y;
}
}
/* Plutôt que d'accèder par indice à lpPoints, on fait progresser un pointeur */
void __stdcall UpdatePointsMovePointer(MY_POINT *lpPoints, int nPointCount, MY_POINT *lpDelta)
{
register int i, j, k;
register MY_POINT *ptr = lpPoints ;
j = lpDelta->x ;
k = lpDelta->y ;
for (i = 0; i < nPointCount; i++)
{
ptr->x += j;
ptr->y += k;
ptr++ ;
}
}
/* Code assembleur rechargeant depuis lpDelta */
__declspec(naked) void __stdcall UpdatePointsAsmReloadDelta(MY_POINT *lpPoints, int nPointCount, MY_POINT *lpDelta)
{
__asm
{
push edi
push esi
mov ecx, [esp + 16] /* ecx = nPointCount */
mov esi, [esp + 20] /* esi = lpDelta */
mov edi, [esp + 12] /* edi = lpPoints */
next:
jecxz the_end /* Fin de loop si i = 0 */
dec ecx /* i-- */
mov eax, [esi] /* eax = lpDelta.x */
mov edx, [esi + 4] /* edx = lpDelta.y */
add [edi + ecx * 8], eax /* lpPoints[i].x = eax */
add [edi + ecx * 8 + 4], edx /* lpPoints[i].y = edx */
jmp next
the_end:
pop esi
pop edi
ret 12
}
}
/* Code assembleur utilisant le scale */
__declspec(naked) void __stdcall UpdatePointsAsmWithScale(MY_POINT *lpPoints, int nPointCount, MY_POINT *lpDelta)
{
__asm
{
push edi
mov ecx, [esp + 12] /* ecx = nPointCount */
mov edi, [esp + 16] /* edi = lpDelta */
mov eax, [edi] /* eax = lpDelta.x */
mov edx, [edi + 4] /* edx = lpDelta.y */
mov edi, [esp + 8] /* edi = lpPoints */
next:
jecxz the_end /* Fin de loop si i = 0 */
dec ecx /* i-- */
add [edi + ecx * 8], eax /* lpPoints[i].x = eax */
add [edi + ecx * 8 + 4], edx /* lpPoints[i].y = edx */
jmp next
the_end:
pop edi
ret 12
}
}
/* Code assembleur faisant progresser le pointeur */
__declspec(naked) void __stdcall UpdatePointsAsmMovePointer(MY_POINT *lpPoints, int nPointCount, MY_POINT *lpDelta)
{
__asm
{
mov ecx, [esp + 8] /* ecx = nPointCount */
test ecx, ecx /* Vérification nPointCount */
jz the_end
push edi
mov edi, [esp + 16] /* edi = lpDelta */
mov eax, [edi] /* eax = lpDelta.x */
mov edx, [edi + 4] /* edx = lpDelta.y */
mov edi, [esp + 8] /* edi = lpPoints */
next:
add [edi], eax /* lpPoints->x = eax */
add [edi + 4], edx /* lpPoints->y = edx */
add edi, 8 /* lpPoints++ */
dec ecx /* i-- */
jnz next /* loop si i > 0 */
pop edi
the_end:
ret 12
}
}
/* Liste des fonctions à tester */
FUNCTION_TO_TEST_INFO updatePoints[FUNCTION_TO_TEST_COUNT] =
{
{"UpdatePointsSimple", UpdatePointsSimple, 0},
{"UpdatePointsCopyDelta", UpdatePointsCopyDelta, 0},
{"UpdatePointsMovePointer", UpdatePointsMovePointer, 0},
{"UpdatePointsAsmReloadDelta", UpdatePointsAsmReloadDelta, 0},
{"UpdatePointsAsmWithScale", UpdatePointsAsmWithScale, 0},
{"UpdatePointsAsmMovePointer", UpdatePointsAsmMovePointer, 0}
};
void InitPoints(MY_POINT *lpPoints, int nPointCount)
{
int i;
for (i = 0; i < nPointCount; i++)
{
lpPoints[i].x = i;
lpPoints[i].y = 100 + i;
}
}
int main()
{
MY_POINT* lpPointsReference;
MY_POINT* lpPoints;
MY_POINT delta;
DWORD nBeginning;
DWORD nDuration;
int nResult;
int i, j, k;
nResult = 1;
lpPointsReference = (MY_POINT*)malloc(POINT_COUNT * sizeof(MY_POINT));
if (!lpPointsReference)
{
puts("Malloc failed");
goto the_end;
}
lpPoints = (MY_POINT*)malloc(POINT_COUNT * sizeof(MY_POINT));
if (!lpPoints)
{
puts("Malloc failed");
goto free_reference;
}
delta.x = 1;
delta.y = 2;
/* On fait une référence pour s'assurer des résultats des différentes fonctions */
InitPoints(lpPointsReference, POINT_COUNT);
UpdatePointsSimple(lpPointsReference, POINT_COUNT, &delta);
/* On réalise plusieurs fois le test pour limiter les effets de bords */
for (k = 0; k < TEST_COUNT; k++)
{
printf("*** Test %d ***\n", k);
/* Pour toutes les fonctions à tester */
for (i = 0; i < FUNCTION_TO_TEST_COUNT; i++)
{
InitPoints(lpPoints, POINT_COUNT);
printf("Testing %s\n", updatePoints[i].name);
nBeginning = GetTickCount();
updatePoints[i].function(lpPoints, POINT_COUNT, &delta);
nDuration = GetTickCount() - nBeginning;
updatePoints[i].duration += nDuration;
printf("%d\n", nDuration);
/* Vérification du résultat */
for (j = 0; j < POINT_COUNT; j++)
if ((lpPointsReference[j].x != lpPoints[j].x) || (lpPointsReference[j].y != lpPoints[j].y))
{
puts("Difference between result and reference");
goto free_points;
}
}
puts("");
}
/* Affichage des temps moyens */
for (i = 0; i < FUNCTION_TO_TEST_COUNT; i++)
{
printf("%s average duration = %d\n", updatePoints[i].name, updatePoints[i].duration / TEST_COUNT);
}
nResult = 0;
free_points:
free(lpPoints);
free_reference:
free(lpPointsReference);
the_end:
return nResult;
} |
Partager