diff options
Diffstat (limited to 'benchmark/bench.c')
| -rw-r--r-- | benchmark/bench.c | 180 |
1 files changed, 33 insertions, 147 deletions
diff --git a/benchmark/bench.c b/benchmark/bench.c index cb2f75c..2c3358f 100644 --- a/benchmark/bench.c +++ b/benchmark/bench.c | |||
| @@ -6,132 +6,16 @@ | |||
| 6 | 6 | ||
| 7 | #include "../cube.h" | 7 | #include "../cube.h" |
| 8 | 8 | ||
| 9 | #define MOVES 100000000 | 9 | #define MOVES 100000000 |
| 10 | #define TRANS 100000000 | 10 | #define TRANS 100000000 |
| 11 | #define COMP 100000000 | 11 | #define COMPOSE 100000000 |
| 12 | #define INV 100000000 | 12 | #define INVERSE 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 | 13 | ||
| 132 | double | 14 | double |
| 133 | bench(void (*run)(void), void (*setup)(void), char *name) | 15 | bench(cube_t (*run)(int64_t), int64_t n, char *name) |
| 134 | { | 16 | { |
| 17 | char str[1000]; | ||
| 18 | cube_t cube; | ||
| 135 | struct timespec start, end; | 19 | struct timespec start, end; |
| 136 | double tdiff, tdsec, tdnano; | 20 | double tdiff, tdsec, tdnano; |
| 137 | 21 | ||
| @@ -144,14 +28,16 @@ bench(void (*run)(void), void (*setup)(void), char *name) | |||
| 144 | return -1.0; | 28 | return -1.0; |
| 145 | } | 29 | } |
| 146 | 30 | ||
| 147 | printf("> %s: setting up benchmark...\n", name); | ||
| 148 | fflush(stdout); | ||
| 149 | if (setup != NULL) | ||
| 150 | setup(); | ||
| 151 | printf("> %s: running benchmark...\n", name); | 31 | printf("> %s: running benchmark...\n", name); |
| 152 | fflush(stdout); | 32 | fflush(stdout); |
| 153 | clock_gettime(CLOCK_MONOTONIC, &start); | 33 | clock_gettime(CLOCK_MONOTONIC, &start); |
| 154 | run(); | 34 | |
| 35 | cube = run(n); | ||
| 36 | writecube("H48", cube, str); | ||
| 37 | str[3] = 0; | ||
| 38 | printf("> %s: resulting cube, first piece: %s\n", name, str); | ||
| 39 | fflush(stdout); | ||
| 40 | |||
| 155 | clock_gettime(CLOCK_MONOTONIC, &end); | 41 | clock_gettime(CLOCK_MONOTONIC, &end); |
| 156 | tdsec = end.tv_sec - start.tv_sec; | 42 | tdsec = end.tv_sec - start.tv_sec; |
| 157 | tdnano = end.tv_nsec - start.tv_nsec; | 43 | tdnano = end.tv_nsec - start.tv_nsec; |
| @@ -163,34 +49,34 @@ bench(void (*run)(void), void (*setup)(void), char *name) | |||
| 163 | } | 49 | } |
| 164 | 50 | ||
| 165 | int main() { | 51 | int main() { |
| 166 | double tmoves, ttrans, tcomp, tinv; | 52 | double tmoves, ttrans, tcompose, tinverse; |
| 167 | 53 | ||
| 168 | printf( | 54 | printf( |
| 169 | "Benchmarks settings:\n" | 55 | "Benchmarks settings:\n" |
| 170 | "MOVES:\t%d\nTRANS:\t%d\nCOMP:\t%d\nINV:\t%d\n", | 56 | "MOVES:\t%d\nTRANS:\t%d\nCOMPOSE:\t%d\nINVERSE:\t%d\n", |
| 171 | MOVES, TRANS, COMP, INV | 57 | MOVES, TRANS, COMPOSE, INVERSE |
| 172 | ); | 58 | ); |
| 173 | fflush(stdout); | 59 | fflush(stdout); |
| 174 | 60 | ||
| 175 | srand(time(NULL)); | 61 | srand(time(NULL)); |
| 176 | 62 | ||
| 177 | tmoves = bench(run_moves, setup_moves, "moves"); | 63 | tmoves = bench(run_moves, MOVES, "moves"); |
| 178 | ttrans = bench(run_trans, setup_trans, "trans"); | 64 | ttrans = bench(run_trans, TRANS, "trans"); |
| 179 | tcomp = bench(run_comp, setup_cubes, "comp"); | 65 | tcompose = bench(run_compose, COMPOSE, "compose"); |
| 180 | tinv = bench(run_inv, setup_cubes, "inv"); | 66 | tinverse = bench(run_inverse, INVERSE, "inverse"); |
| 181 | 67 | ||
| 182 | printf( | 68 | printf( |
| 183 | "\nBenchmark summary:\n" | 69 | "\nBenchmark summary:\n" |
| 184 | "moves: %d moves in %.4fs (%.4f MTPS)\n" | 70 | "moves: %d moves in %.4fs (%.4f MTPS)\n" |
| 185 | "trans: %d trans in %.4fs (%.4f MTPS)\n" | 71 | "trans: %d transformations in %.4fs (%.4f MTPS)\n" |
| 186 | "comp: %d comps in %.4fs (%.4f MCPS)\n" | 72 | "compose: %d compositions in %.4fs (%.4f MCPS)\n" |
| 187 | "inv: %d invs in %.4fs (%.4f MIPS)\n" | 73 | "inverse: %d inverses in %.4fs (%.4f MIPS)\n" |
| 188 | "Total time: %.4f\n", | 74 | "Total time: %.4f\n", |
| 189 | MOVES, tmoves, MOVES / (1e6 * tmoves), | 75 | MOVES, tmoves, MOVES / (1e6 * tmoves), |
| 190 | TRANS, ttrans, TRANS / (1e6 * ttrans), | 76 | TRANS, ttrans, TRANS / (1e6 * ttrans), |
| 191 | COMP, tcomp, COMP / (1e6 * tcomp), | 77 | COMPOSE, tcompose, COMPOSE / (1e6 * tcompose), |
| 192 | INV, tinv, INV / (1e6 * tinv), | 78 | INVERSE, tinverse, INVERSE / (1e6 * tinverse), |
| 193 | tmoves + ttrans + tcomp + tinv | 79 | tmoves + ttrans + tcompose + tinverse |
| 194 | ); | 80 | ); |
| 195 | 81 | ||
| 196 | return 0; | 82 | return 0; |
