diff options
Diffstat (limited to 'benchmark')
| -rw-r--r-- | benchmark/bench.c | 188 | ||||
| -rwxr-xr-x | benchmark/bench.sh | 2 |
2 files changed, 189 insertions, 1 deletions
diff --git a/benchmark/bench.c b/benchmark/bench.c index 046e965..3046cdc 100644 --- a/benchmark/bench.c +++ b/benchmark/bench.c | |||
| @@ -1,11 +1,197 @@ | |||
| 1 | #include <stdbool.h> | 1 | #include <stdbool.h> |
| 2 | #include <stdint.h> | 2 | #include <stdint.h> |
| 3 | #include <stdio.h> | 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> | ||
| 5 | #include <time.h> | ||
| 4 | 6 | ||
| 5 | #include "../src/cube.h" | 7 | #include "../src/cube.h" |
| 6 | 8 | ||
| 9 | #define MOVES 100000000 | ||
| 10 | #define TRANS 100000000 | ||
| 11 | #define COMP 100000000 | ||
| 12 | #define INV 100000000 | ||
| 13 | |||
| 14 | #define GEN_MOVES 10000 | ||
| 15 | #define GEN_TRANS 10000 | ||
| 16 | #define GEN_CUBES 10000 | ||
| 17 | |||
| 18 | static move_t gen_moves[GEN_MOVES]; | ||
| 19 | static trans_t gen_trans[GEN_TRANS]; | ||
| 20 | static cube_t gen_cubes[GEN_CUBES]; | ||
| 21 | |||
| 22 | void setup_moves(void); | ||
| 23 | void setup_trans(void); | ||
| 24 | void setup_cube(void); | ||
| 25 | void run_moves(void); | ||
| 26 | void run_trans(void); | ||
| 27 | void run_comp(void); | ||
| 28 | void run_inv(void); | ||
| 29 | double bench(void (*)(void), void (*)(void), char *); | ||
| 30 | |||
| 31 | void | ||
| 32 | setup_moves(void) | ||
| 33 | { | ||
| 34 | int i; | ||
| 35 | |||
| 36 | for (i = 0; i < GEN_MOVES; i++) | ||
| 37 | gen_moves[i] = (move_t)rand() % 18; | ||
| 38 | } | ||
| 39 | |||
| 40 | void | ||
| 41 | setup_trans(void) | ||
| 42 | { | ||
| 43 | int i; | ||
| 44 | |||
| 45 | for (i = 0; i < GEN_TRANS; i++) | ||
| 46 | gen_trans[i] = (trans_t)rand() % 48; | ||
| 47 | } | ||
| 48 | |||
| 49 | void | ||
| 50 | setup_cubes(void) | ||
| 51 | { | ||
| 52 | int i, j; | ||
| 53 | move_t m; | ||
| 54 | |||
| 55 | for (i = 0; i < GEN_CUBES; i++) { | ||
| 56 | gen_cubes[i] = solvedcube; | ||
| 57 | for (j = 0; j < 30; j++) { | ||
| 58 | m = (move_t)rand() % 18; | ||
| 59 | gen_cubes[i] = move(gen_cubes[i], m); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | void | ||
| 65 | run_moves(void) | ||
| 66 | { | ||
| 67 | int i; | ||
| 68 | cube_t c; | ||
| 69 | char str[1000]; | ||
| 70 | |||
| 71 | c = solvedcube; | ||
| 72 | for (i = 0; i < MOVES; i++) | ||
| 73 | c = move(c, gen_moves[i % GEN_MOVES]); | ||
| 74 | |||
| 75 | writecube(H48, c, str); | ||
| 76 | str[3] = 0; | ||
| 77 | printf("> moves: resulting cube, first piece: %s\n", str); | ||
| 78 | fflush(stdout); | ||
| 79 | } | ||
| 80 | |||
| 81 | void | ||
| 82 | run_trans(void) | ||
| 83 | { | ||
| 84 | int i; | ||
| 85 | cube_t c; | ||
| 86 | char str[1000]; | ||
| 87 | |||
| 88 | c = solvedcube; | ||
| 89 | for (i = 0; i < TRANS; i++) | ||
| 90 | c = transform(c, gen_trans[i % GEN_TRANS]); | ||
| 91 | |||
| 92 | writecube(H48, c, str); | ||
| 93 | str[3] = 0; | ||
| 94 | printf("> trans: resulting cube, first piece: %s\n", str); | ||
| 95 | fflush(stdout); | ||
| 96 | } | ||
| 97 | |||
| 98 | void | ||
| 99 | run_comp(void) | ||
| 100 | { | ||
| 101 | int i; | ||
| 102 | cube_t c; | ||
| 103 | char str[1000]; | ||
| 104 | |||
| 105 | c = solvedcube; | ||
| 106 | for (i = 0; i < COMP; i++) | ||
| 107 | c = compose(c, gen_cubes[i % GEN_CUBES]); | ||
| 108 | |||
| 109 | writecube(H48, c, str); | ||
| 110 | str[3] = 0; | ||
| 111 | printf("> comp: resulting cube, first piece: %s\n", str); | ||
| 112 | fflush(stdout); | ||
| 113 | } | ||
| 114 | |||
| 115 | void | ||
| 116 | run_inv(void) | ||
| 117 | { | ||
| 118 | int i, j; | ||
| 119 | char str[1000]; | ||
| 120 | |||
| 121 | for (i = 0; i < COMP; i++) { | ||
| 122 | j = i % (GEN_CUBES-1); | ||
| 123 | gen_cubes[j] = inverse(gen_cubes[j+1]); | ||
| 124 | } | ||
| 125 | |||
| 126 | writecube(H48, gen_cubes[0], str); | ||
| 127 | str[3] = 0; | ||
| 128 | printf("> comp: resulting cube, first piece: %s\n", str); | ||
| 129 | fflush(stdout); | ||
| 130 | } | ||
| 131 | |||
| 132 | double | ||
| 133 | bench(void (*run)(void), void (*setup)(void), char *name) | ||
| 134 | { | ||
| 135 | struct timespec start, end; | ||
| 136 | double tdiff, tdsec, tdnano; | ||
| 137 | |||
| 138 | printf("\n"); | ||
| 139 | fflush(stdout); | ||
| 140 | |||
| 141 | if (run == NULL) { | ||
| 142 | printf("> %s: nothing to run!\n", name); | ||
| 143 | fflush(stdout); | ||
| 144 | return -1.0; | ||
| 145 | } | ||
| 146 | |||
| 147 | printf("> %s: setting up benchmark...\n", name); | ||
| 148 | fflush(stdout); | ||
| 149 | if (setup != NULL) | ||
| 150 | setup(); | ||
| 151 | printf("> %s: running benchmark...\n", name); | ||
| 152 | fflush(stdout); | ||
| 153 | clock_gettime(CLOCK_MONOTONIC, &start); | ||
| 154 | run(); | ||
| 155 | clock_gettime(CLOCK_MONOTONIC, &end); | ||
| 156 | tdsec = end.tv_sec - start.tv_sec; | ||
| 157 | tdnano = end.tv_nsec - start.tv_nsec; | ||
| 158 | tdiff = tdsec + 1e-9 * tdnano; | ||
| 159 | printf("> %s: %.4fs\n", name, tdiff); | ||
| 160 | fflush(stdout); | ||
| 161 | |||
| 162 | return tdiff; | ||
| 163 | } | ||
| 164 | |||
| 7 | int main() { | 165 | int main() { |
| 8 | printf("Benchmarks not yet set up\n"); | 166 | double tmoves, ttrans, tcomp, tinv; |
| 167 | |||
| 168 | printf( | ||
| 169 | "Benchmarks settings:\n" | ||
| 170 | "MOVES:\t%d\nTRANS:\t%d\nCOMP:\t%d\nINV:\t%d\n", | ||
| 171 | MOVES, TRANS, COMP, INV | ||
| 172 | ); | ||
| 173 | fflush(stdout); | ||
| 174 | |||
| 175 | srand(time(NULL)); | ||
| 176 | |||
| 177 | tmoves = bench(run_moves, setup_moves, "moves"); | ||
| 178 | ttrans = bench(run_trans, setup_trans, "trans"); | ||
| 179 | tcomp = bench(run_comp, setup_cubes, "comp"); | ||
| 180 | tinv = bench(run_inv, setup_cubes, "inv"); | ||
| 181 | |||
| 182 | printf( | ||
| 183 | "\nBenchmark summary:\n" | ||
| 184 | "moves: %d moves in %.4fs (%.4f MTPS)\n" | ||
| 185 | "trans: %d trans in %.4fs (%.4f MTPS)\n" | ||
| 186 | "comp: %d comps in %.4fs (%.4f MCPS)\n" | ||
| 187 | "inv: %d invs in %.4fs (%.4f MIPS)\n" | ||
| 188 | "Total time: %.4f\n", | ||
| 189 | MOVES, tmoves, MOVES / (1e6 * tmoves), | ||
| 190 | TRANS, ttrans, TRANS / (1e6 * ttrans), | ||
| 191 | COMP, tcomp, COMP / (1e6 * tcomp), | ||
| 192 | INV, tinv, INV / (1e6 * tinv), | ||
| 193 | tmoves + ttrans + tcomp + tinv | ||
| 194 | ); | ||
| 9 | 195 | ||
| 10 | return 0; | 196 | return 0; |
| 11 | } | 197 | } |
diff --git a/benchmark/bench.sh b/benchmark/bench.sh index 288db05..850909c 100755 --- a/benchmark/bench.sh +++ b/benchmark/bench.sh | |||
| @@ -7,6 +7,8 @@ CUBEOBJ="cube.o" | |||
| 7 | cc -std=c99 -pthread -O3 -o $BENCHBIN benchmark/bench.c $CUBEOBJ || exit 1; | 7 | cc -std=c99 -pthread -O3 -o $BENCHBIN benchmark/bench.c $CUBEOBJ || exit 1; |
| 8 | 8 | ||
| 9 | $BENCHBIN | tee $BENCHOUT | 9 | $BENCHBIN | tee $BENCHOUT |
| 10 | |||
| 11 | echo "" | ||
| 10 | echo "Results saved to $BENCHOUT" | 12 | echo "Results saved to $BENCHOUT" |
| 11 | 13 | ||
| 12 | rm -rf $BENCHBIN $CUBEOBJ | 14 | rm -rf $BENCHBIN $CUBEOBJ |
