diff options
Diffstat (limited to 'tools/401_benchmark_performance')
| -rw-r--r-- | tools/401_benchmark_performance/benchmark_performance.c | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/tools/401_benchmark_performance/benchmark_performance.c b/tools/401_benchmark_performance/benchmark_performance.c new file mode 100644 index 0000000..97cb51a --- /dev/null +++ b/tools/401_benchmark_performance/benchmark_performance.c | |||
| @@ -0,0 +1,183 @@ | |||
| 1 | #include "../tool.h" | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <stdlib.h> | ||
| 4 | #include <string.h> | ||
| 5 | #include <time.h> | ||
| 6 | |||
| 7 | #define SOL_BUFFER_LEN 1000 | ||
| 8 | #define MAX_SCRAMBLES 100 // Limite massimo per il numero di scrambles che può essere letto | ||
| 9 | |||
| 10 | char *scrambles[MAX_SCRAMBLES + 1]; | ||
| 11 | char *solver; | ||
| 12 | int64_t size = 0; | ||
| 13 | char *buf; | ||
| 14 | |||
| 15 | struct ScrambleArgs { | ||
| 16 | int scramble_idx; | ||
| 17 | }; | ||
| 18 | |||
| 19 | // Funzione di timer per un singolo scramble | ||
| 20 | static double timerun_single_scramble(void (*run)(struct ScrambleArgs *, char *, int *), struct ScrambleArgs *args, char *solution, int *solution_length) { | ||
| 21 | struct timespec start, end; | ||
| 22 | double tdiff, tdsec, tdnano; | ||
| 23 | |||
| 24 | fflush(stdout); | ||
| 25 | |||
| 26 | clock_gettime(CLOCK_MONOTONIC, &start); | ||
| 27 | run(args, solution, solution_length); // Esegue la funzione run per il singolo scramble | ||
| 28 | clock_gettime(CLOCK_MONOTONIC, &end); | ||
| 29 | |||
| 30 | tdsec = end.tv_sec - start.tv_sec; | ||
| 31 | tdnano = end.tv_nsec - start.tv_nsec; | ||
| 32 | tdiff = tdsec + 1e-9 * tdnano; | ||
| 33 | |||
| 34 | return tdiff; | ||
| 35 | } | ||
| 36 | |||
| 37 | // Modifica della funzione run_scramble per evitare ricorsione | ||
| 38 | void run_scramble(struct ScrambleArgs *args, char *solution, int *solution_length) { | ||
| 39 | int scramble_idx = args->scramble_idx; // Estrai l'indice dello scramble | ||
| 40 | int64_t n; | ||
| 41 | char sol[SOL_BUFFER_LEN], cube[22]; | ||
| 42 | long long stats[NISSY_SIZE_SOLVE_STATS]; | ||
| 43 | |||
| 44 | printf("%d. %s\n", scramble_idx + 1, scrambles[scramble_idx]); | ||
| 45 | printf("Solving scramble %s\n", scrambles[scramble_idx]); | ||
| 46 | |||
| 47 | if (nissy_applymoves(NISSY_SOLVED_CUBE, scrambles[scramble_idx], cube) == -1) { | ||
| 48 | printf("Invalid scramble\n"); | ||
| 49 | strcpy(solution, "Invalid scramble"); | ||
| 50 | *solution_length = 0; | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | |||
| 54 | n = nissy_solve(cube, solver, NISSY_NISSFLAG_NORMAL, 0, 20, 1, -1, size, buf, SOL_BUFFER_LEN, sol, stats); | ||
| 55 | if (n == 0) { | ||
| 56 | printf("No solution found\n"); | ||
| 57 | strcpy(solution, "No solution found"); | ||
| 58 | *solution_length = 0; | ||
| 59 | } else { | ||
| 60 | printf("Solutions:\n%s\n", sol); | ||
| 61 | strcpy(solution, sol); | ||
| 62 | *solution_length = nissy_countmoves(solution); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | void run_all_scrambles(void) { | ||
| 67 | double total_time = 0.0, average_time = 0.0; | ||
| 68 | |||
| 69 | printf("Solved the following scrambles with timings:\n\n"); | ||
| 70 | |||
| 71 | FILE *output_file = fopen("./tools/401_benchmark_performance/compressed_results.txt", "w"); | ||
| 72 | if (!output_file) { | ||
| 73 | printf("Error: Could not open output file for writing\n"); | ||
| 74 | return; | ||
| 75 | } | ||
| 76 | |||
| 77 | int i; | ||
| 78 | for (i = 0; scrambles[i] != NULL; i++) { | ||
| 79 | struct ScrambleArgs args; | ||
| 80 | args.scramble_idx = i; | ||
| 81 | |||
| 82 | char solution[SOL_BUFFER_LEN]; | ||
| 83 | int solution_length = 0; | ||
| 84 | |||
| 85 | double scramble_time = timerun_single_scramble(run_scramble, &args, solution, &solution_length); | ||
| 86 | total_time += scramble_time; | ||
| 87 | |||
| 88 | fprintf(output_file, "%d. %s\n", i + 1, scrambles[i]); | ||
| 89 | fprintf(output_file, "Solution: %s\n", solution); | ||
| 90 | fprintf(output_file, "Length of solution: %d\n", solution_length); | ||
| 91 | fprintf(output_file, "Time for scramble %d: %.4fs\n", i + 1, scramble_time); | ||
| 92 | fprintf(output_file, "-------------------------\n"); | ||
| 93 | |||
| 94 | printf("Time for scramble %d: %.4fs\n", i + 1, scramble_time); | ||
| 95 | printf("Lenght: %d\n", solution_length); | ||
| 96 | } | ||
| 97 | |||
| 98 | if (i > 0) { | ||
| 99 | average_time = total_time / i; | ||
| 100 | } | ||
| 101 | |||
| 102 | printf("---------\n"); | ||
| 103 | printf("Total time for all scrambles: %.4fs\n", total_time); | ||
| 104 | printf("Average time per scramble: %.4fs\n", average_time); | ||
| 105 | |||
| 106 | fprintf(output_file, "---------\n"); | ||
| 107 | fprintf(output_file, "Total time for all scrambles: %.4fs\n", total_time); | ||
| 108 | fprintf(output_file, "Average time per scramble: %.4fs\n", average_time); | ||
| 109 | |||
| 110 | fclose(output_file); | ||
| 111 | } | ||
| 112 | |||
| 113 | int read_scrambles_from_file(const char *filename, char **scrambles) { | ||
| 114 | FILE *file = fopen(filename, "r"); | ||
| 115 | if (!file) { | ||
| 116 | printf("Error: Could not open file %s\n", filename); | ||
| 117 | return -1; | ||
| 118 | } | ||
| 119 | |||
| 120 | char line[256]; | ||
| 121 | int count = 0; | ||
| 122 | while (fgets(line, sizeof(line), file)) { | ||
| 123 | line[strcspn(line, "\n")] = 0; | ||
| 124 | |||
| 125 | if (strncmp(line, "//", 2) == 0 || strlen(line) == 0) | ||
| 126 | continue; | ||
| 127 | |||
| 128 | scrambles[count] = (char *)malloc(strlen(line) + 1); | ||
| 129 | if (scrambles[count] == NULL) { | ||
| 130 | printf("Error: Memory allocation failed\n"); | ||
| 131 | fclose(file); | ||
| 132 | return -1; | ||
| 133 | } | ||
| 134 | strcpy(scrambles[count], line); | ||
| 135 | count++; | ||
| 136 | |||
| 137 | if (count >= MAX_SCRAMBLES) { | ||
| 138 | printf("Warning: Reached maximum number of scrambles (%d)\n", MAX_SCRAMBLES); | ||
| 139 | break; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | scrambles[count] = NULL; | ||
| 144 | fclose(file); | ||
| 145 | return 0; | ||
| 146 | } | ||
| 147 | |||
| 148 | int main(int argc, char **argv) { | ||
| 149 | char tables_filename[255]; | ||
| 150 | char scrambles_filename[255] = "./tools/401_benchmark_performance/scrambles.txt"; | ||
| 151 | |||
| 152 | if (argc < 2) { | ||
| 153 | printf("Error: not enough arguments. A solver must be given.\n"); | ||
| 154 | return 1; | ||
| 155 | } | ||
| 156 | |||
| 157 | solver = argv[1]; | ||
| 158 | |||
| 159 | srand(time(NULL)); | ||
| 160 | nissy_setlogger(log_stderr); | ||
| 161 | |||
| 162 | sprintf(tables_filename, "tables/%s", solver); | ||
| 163 | if (getdata(solver, &buf, tables_filename) != 0) | ||
| 164 | return 1; | ||
| 165 | |||
| 166 | size = nissy_datasize(solver); | ||
| 167 | |||
| 168 | if (read_scrambles_from_file(scrambles_filename, scrambles) != 0) { | ||
| 169 | printf("Error: failed to read scrambles from file\n"); | ||
| 170 | free(buf); | ||
| 171 | return 1; | ||
| 172 | } | ||
| 173 | |||
| 174 | run_all_scrambles(); | ||
| 175 | |||
| 176 | free(buf); | ||
| 177 | |||
| 178 | for (int i = 0; scrambles[i] != NULL; i++) { | ||
| 179 | free(scrambles[i]); | ||
| 180 | } | ||
| 181 | |||
| 182 | return 0; | ||
| 183 | } | ||
