Debian Codegraph @

research/compare_mod_and.c

raw symbols SCIP
1 /*
2 Simple hack to compare performance of % and & on modern
3 machines. Seems that & still wins on a wide variety of
4 machines, so we'll stick with it for DASM's compression
5 step in hashing.
6
7 1.5 GHz PowerPC G4:
8
9 -O3, MAX 1<<31
10
11 Takes 5 seconds to do 2147483648*4 modulos.
12 ? Takes 5 seconds to do 2147483648*4 ands.
13 ? Takes 3 seconds to do 2147483648*4 ands and minuses.
14
15 -O0, MAX 1<<27
16
17 Takes 11 seconds to do 134217728*4 modulos.
18 Takes 3 seconds to do 134217728*4 ands.
19 ? Takes 2 seconds to do 134217728*4 ands and minuses.
20
21 ugradx i686:
22
23 -O3, MAX 1<<31
24
25 Takes 2 seconds to do 2147483648*4 modulos.
26 Takes 1 seconds to do 2147483648*4 ands.
27 Takes 1 seconds to do 2147483648*4 ands and minuses.
28
29 -O0, MAX 1<<27
30
31 Takes 8 seconds to do 134217728*4 modulos.
32 Takes 1 seconds to do 134217728*4 ands.
33 Takes 1 seconds to do 134217728*4 ands and minuses.
34 */
35
36 #include <stdio.h>
37 #include <time.h>
38
39 #define MAX 1<<27
40
41 double time_mod(void)
42 {
43 unsigned a, b, c, i, d = 13;
44 time_t before, after;
45
46 before = time(NULL);
47
48 for (i = 0; i < MAX; i++) {
49 a = i % d;
50 b = i % d;
51 c = i % d;
52 a = i % d;
53 }
54
55 after = time(NULL);
56 return difftime(after, before);
57 }
58
59 double time_and(void)
60 {
61 unsigned a, b, c, i, d = 13;
62 time_t before, after;
63
64 before = time(NULL);
65
66 for (i = 0; i < MAX; i++) {
67 a = i & d;
68 b = i & d;
69 c = i & d;
70 a = i & d;
71 }
72
73 after = time(NULL);
74 return difftime(after, before);
75 }
76
77 double time_and_minus(void)
78 {
79 unsigned a, b, c, i, d = 13;
80 time_t before, after;
81
82 before = time(NULL);
83
84 for (i = 0; i < MAX; i++) {
85 a = i & (d-1);
86 b = i & (d-1);
87 c = i & (d-1);
88 a = i & (d-1);
89 }
90
91 after = time(NULL);
92 return difftime(after, before);
93 }
94
95 /*
96 Added approximations of the actual hash functions that
97 would be used. Seems that Dan's is a *tiny* bit slower
98 (figures since there's an extra + in there) but in the
99 actual program we have to weigh better distribution of
100 hash values against that. Still to actually measure...
101 */
102
103 double time_dillon(void)
104 {
105 unsigned a = 0, b, d = 13, *c = &d, i;
106 time_t before, after;
107
108 before = time(NULL);
109
110 for (i = 0; i < MAX; i++) {
111 a = (a << 2) + *c; /* ++ is missing :-/ */
112 a = (a << 2) + *c;
113 a = (a << 2) + *c;
114 a = (a << 2) + *c;
115 }
116
117 after = time(NULL);
118 return difftime(after, before);
119 }
120
121 double time_bernstein(void)
122 {
123 unsigned a = 0, b, d = 13, *c = &d, i;
124 time_t before, after;
125
126 before = time(NULL);
127
128 for (i = 0; i < MAX; i++) {
129 a = ((a << 5) + a) + *c; /* ++ is missing :-/ */
130 a = ((a << 5) + a) + *c; /* ++ is missing :-/ */
131 a = ((a << 5) + a) + *c; /* ++ is missing :-/ */
132 a = ((a << 5) + a) + *c; /* ++ is missing :-/ */
133 }
134
135 after = time(NULL);
136 return difftime(after, before);
137 }
138
139 int main(void)
140 {
141 printf("Takes %g seconds to do %u*4 modulos.\n",
142 time_mod(), MAX);
143 printf("Takes %g seconds to do %u*4 ands.\n",
144 time_and(), MAX);
145 printf("Takes %g seconds to do %u*4 ands and minuses.\n",
146 time_and_minus(), MAX);
147 printf("Takes %g seconds to do %u*4 dillons.\n",
148 time_dillon(), MAX);
149 printf("Takes %g seconds to do %u*4 bernsteins.\n",
150 time_bernstein(), MAX);
151 }