-
Notifications
You must be signed in to change notification settings - Fork 2
/
time_loop.c
52 lines (47 loc) · 1.11 KB
/
time_loop.c
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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "tdiff.h"
int main(void) {
struct timespec clock1, clock2;
puts("loop:");
clock_gettime(CLOCK_MONOTONIC, &clock1);
__asm__("loopl .\n\t" :: "c"(-1));
clock_gettime(CLOCK_MONOTONIC, &clock2);
printf("%8lli ms\n", tdiff(clock1, clock2)/1000000);
puts("dec + jnz:");
clock_gettime(CLOCK_MONOTONIC, &clock1);
__asm__(
"1: decl %%ecx\n\t"
"jnz 1b\n\t"
:: "c"(-1)
);
clock_gettime(CLOCK_MONOTONIC, &clock2);
printf("%8lli ms\n", tdiff(clock1, clock2)/1000000);
puts("test + jz + dec + jmp:");
clock_gettime(CLOCK_MONOTONIC, &clock1);
__asm__(
"1: test %%ecx, %%ecx\n\t"
"jz 1f\n\t"
"decl %%ecx\n\t"
"jmp 1b\n\t"
"1:\n\t"
:: "c"(-1)
);
clock_gettime(CLOCK_MONOTONIC, &clock2);
printf("%8lli ms\n", tdiff(clock1, clock2)/1000000);
puts("jcxz + dec + jmp:");
clock_gettime(CLOCK_MONOTONIC, &clock1);
__asm__(
"1:\n\t"
"jecxz 1f\n\t"
"decl %%ecx\n\t"
"jmp 1b\n\t"
"1:\n\t"
:: "c"(-1)
);
clock_gettime(CLOCK_MONOTONIC, &clock2);
printf("%8lli ms\n", tdiff(clock1, clock2)/1000000);
return EXIT_SUCCESS;
}