diff options
| -rw-r--r-- | README.md | 8 | ||||
| -rw-r--r-- | benchmark/bench.c | 180 | ||||
| -rwxr-xr-x | benchmark/bench.sh | 2 | ||||
| -rw-r--r-- | benchmark/cube-bench.c | 147 |
4 files changed, 182 insertions, 155 deletions
| @@ -26,10 +26,4 @@ regex, for example: | |||
| 26 | $ TEST=coord make test | 26 | $ TEST=coord make test |
| 27 | ``` | 27 | ``` |
| 28 | 28 | ||
| 29 | You can also run | 29 | Due to ongoing changes, benchmarks are currently broken. |
| 30 | |||
| 31 | ``` | ||
| 32 | $ make benchmark | ||
| 33 | ``` | ||
| 34 | |||
| 35 | for benchmarks. | ||
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; |
diff --git a/benchmark/bench.sh b/benchmark/bench.sh index ec850f0..cc31c1e 100755 --- a/benchmark/bench.sh +++ b/benchmark/bench.sh | |||
| @@ -9,7 +9,7 @@ BENCHBIN="benchmark/run" | |||
| 9 | BENCHDIR="benchmark/results" | 9 | BENCHDIR="benchmark/results" |
| 10 | CUBEOBJ="cube.o" | 10 | CUBEOBJ="cube.o" |
| 11 | 11 | ||
| 12 | $CC -o $BENCHBIN benchmark/bench.c $CUBEOBJ || exit 1; | 12 | $CC -D_POSIX_C_SOURCE=199309L -o $BENCHBIN benchmark/bench.c $CUBEOBJ || exit 1 |
| 13 | 13 | ||
| 14 | d="$(date +'%Y-%m-%d-%H-%M-%S')" | 14 | d="$(date +'%Y-%m-%d-%H-%M-%S')" |
| 15 | mkdir -p "$BENCHDIR" | 15 | mkdir -p "$BENCHDIR" |
diff --git a/benchmark/cube-bench.c b/benchmark/cube-bench.c new file mode 100644 index 0000000..26d12a4 --- /dev/null +++ b/benchmark/cube-bench.c | |||
| @@ -0,0 +1,147 @@ | |||
| 1 | /****************************************************************************** | ||
| 2 | Section: benchmarks | ||
| 3 | |||
| 4 | Here you can find some simple functions that can be used to benchmark the | ||
| 5 | rest of the code. | ||
| 6 | ******************************************************************************/ | ||
| 7 | |||
| 8 | #define RANDOMCUBES 157 | ||
| 9 | |||
| 10 | static void | ||
| 11 | setup_randomcubes(cube_fast_t *cubes) | ||
| 12 | { | ||
| 13 | int i; | ||
| 14 | |||
| 15 | for (i = 0; i < RANDOMCUBES; i++) | ||
| 16 | cubes[i] = run_moves(i*4); | ||
| 17 | } | ||
| 18 | |||
| 19 | cube_t | ||
| 20 | run_moves(int64_t n) | ||
| 21 | { | ||
| 22 | cube_fast_t fast; | ||
| 23 | int64_t m, i; | ||
| 24 | |||
| 25 | fast = solvedcube(); | ||
| 26 | m = n / 18; | ||
| 27 | |||
| 28 | for (i = 0; i < m; i++) { | ||
| 29 | fast = _move_U(fast); | ||
| 30 | fast = _move_U2(fast); | ||
| 31 | fast = _move_U3(fast); | ||
| 32 | fast = _move_D(fast); | ||
| 33 | fast = _move_D2(fast); | ||
| 34 | fast = _move_D3(fast); | ||
| 35 | fast = _move_R(fast); | ||
| 36 | fast = _move_R2(fast); | ||
| 37 | fast = _move_R3(fast); | ||
| 38 | fast = _move_L(fast); | ||
| 39 | fast = _move_L2(fast); | ||
| 40 | fast = _move_L3(fast); | ||
| 41 | fast = _move_F(fast); | ||
| 42 | fast = _move_F2(fast); | ||
| 43 | fast = _move_F3(fast); | ||
| 44 | fast = _move_B(fast); | ||
| 45 | fast = _move_B2(fast); | ||
| 46 | fast = _move_B3(fast); | ||
| 47 | } | ||
| 48 | |||
| 49 | for (i = m * 18; i < n; i++) | ||
| 50 | fast = _move_F(fast); | ||
| 51 | |||
| 52 | return fast; | ||
| 53 | } | ||
| 54 | |||
| 55 | cube_t | ||
| 56 | run_trans(int64_t n) | ||
| 57 | { | ||
| 58 | cube_fast_t fast; | ||
| 59 | int64_t m, i; | ||
| 60 | |||
| 61 | fast = run_moves(33); | ||
| 62 | m = n / 18; | ||
| 63 | |||
| 64 | for (i = 0; i < m; i++) { | ||
| 65 | fast = _trans_UFr(fast); | ||
| 66 | fast = _trans_ULr(fast); | ||
| 67 | fast = _trans_UBr(fast); | ||
| 68 | fast = _trans_URr(fast); | ||
| 69 | fast = _trans_DFr(fast); | ||
| 70 | fast = _trans_DLr(fast); | ||
| 71 | fast = _trans_DBr(fast); | ||
| 72 | fast = _trans_DRr(fast); | ||
| 73 | fast = _trans_RUr(fast); | ||
| 74 | fast = _trans_RFr(fast); | ||
| 75 | fast = _trans_RDr(fast); | ||
| 76 | fast = _trans_RBr(fast); | ||
| 77 | fast = _trans_LUr(fast); | ||
| 78 | fast = _trans_LFr(fast); | ||
| 79 | fast = _trans_LDr(fast); | ||
| 80 | fast = _trans_LBr(fast); | ||
| 81 | fast = _trans_FUr(fast); | ||
| 82 | fast = _trans_FRr(fast); | ||
| 83 | fast = _trans_FDr(fast); | ||
| 84 | fast = _trans_FLr(fast); | ||
| 85 | fast = _trans_BUr(fast); | ||
| 86 | fast = _trans_BRr(fast); | ||
| 87 | fast = _trans_BDr(fast); | ||
| 88 | fast = _trans_BLr(fast); | ||
| 89 | fast = _trans_UFm(fast); | ||
| 90 | fast = _trans_ULm(fast); | ||
| 91 | fast = _trans_UBm(fast); | ||
| 92 | fast = _trans_URm(fast); | ||
| 93 | fast = _trans_DFm(fast); | ||
| 94 | fast = _trans_DLm(fast); | ||
| 95 | fast = _trans_DBm(fast); | ||
| 96 | fast = _trans_DRm(fast); | ||
| 97 | fast = _trans_RUm(fast); | ||
| 98 | fast = _trans_RFm(fast); | ||
| 99 | fast = _trans_RDm(fast); | ||
| 100 | fast = _trans_RBm(fast); | ||
| 101 | fast = _trans_LUm(fast); | ||
| 102 | fast = _trans_LFm(fast); | ||
| 103 | fast = _trans_LDm(fast); | ||
| 104 | fast = _trans_LBm(fast); | ||
| 105 | fast = _trans_FUm(fast); | ||
| 106 | fast = _trans_FRm(fast); | ||
| 107 | fast = _trans_FDm(fast); | ||
| 108 | fast = _trans_FLm(fast); | ||
| 109 | fast = _trans_BUm(fast); | ||
| 110 | fast = _trans_BRm(fast); | ||
| 111 | fast = _trans_BDm(fast); | ||
| 112 | fast = _trans_BLm(fast); | ||
| 113 | } | ||
| 114 | |||
| 115 | for (i = m * 18; i < n; i++) | ||
| 116 | fast = _trans_FRm(fast); | ||
| 117 | |||
| 118 | return fast; | ||
| 119 | } | ||
| 120 | |||
| 121 | cube_t | ||
| 122 | run_compose(int64_t n) | ||
| 123 | { | ||
| 124 | cube_fast_t fast, cubes[RANDOMCUBES]; | ||
| 125 | int64_t i; | ||
| 126 | |||
| 127 | setup_randomcubes(cubes); | ||
| 128 | |||
| 129 | for (i = 0; i < n; i++) | ||
| 130 | fast = compose_fast(fast, cubes[i % RANDOMCUBES]); | ||
| 131 | |||
| 132 | return fast; | ||
| 133 | } | ||
| 134 | |||
| 135 | cube_t | ||
| 136 | run_inverse(int64_t n) | ||
| 137 | { | ||
| 138 | cube_fast_t fast, cubes[RANDOMCUBES]; | ||
| 139 | int64_t i; | ||
| 140 | |||
| 141 | setup_randomcubes(cubes); | ||
| 142 | |||
| 143 | for (i = 0; i < n; i++) | ||
| 144 | fast = inverse_fast(cubes[i % RANDOMCUBES]); | ||
| 145 | |||
| 146 | return fast; | ||
| 147 | } | ||
