diff options
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/401_benchmark_performance/benchmark_performance.c | 114 |
1 files changed, 80 insertions, 34 deletions
diff --git a/tools/401_benchmark_performance/benchmark_performance.c b/tools/401_benchmark_performance/benchmark_performance.c index 97cb51a..97d9bd6 100644 --- a/tools/401_benchmark_performance/benchmark_performance.c +++ b/tools/401_benchmark_performance/benchmark_performance.c | |||
| @@ -5,7 +5,7 @@ | |||
| 5 | #include <time.h> | 5 | #include <time.h> |
| 6 | 6 | ||
| 7 | #define SOL_BUFFER_LEN 1000 | 7 | #define SOL_BUFFER_LEN 1000 |
| 8 | #define MAX_SCRAMBLES 100 // Limite massimo per il numero di scrambles che può essere letto | 8 | #define MAX_SCRAMBLES 200 // Limite massimo per il numero di scrambles che può essere letto |
| 9 | 9 | ||
| 10 | char *scrambles[MAX_SCRAMBLES + 1]; | 10 | char *scrambles[MAX_SCRAMBLES + 1]; |
| 11 | char *solver; | 11 | char *solver; |
| @@ -16,6 +16,26 @@ struct ScrambleArgs { | |||
| 16 | int scramble_idx; | 16 | int scramble_idx; |
| 17 | }; | 17 | }; |
| 18 | 18 | ||
| 19 | double sqrt_approx(double num) { | ||
| 20 | double guess = num / 2.0; // Inizializza una stima approssimata | ||
| 21 | double epsilon = 0.00001; // Precisione dell'approssimazione | ||
| 22 | |||
| 23 | while ((guess * guess - num) > epsilon || (num - guess * guess) > epsilon) { | ||
| 24 | guess = (guess + num / guess) / 2.0; | ||
| 25 | } | ||
| 26 | |||
| 27 | return guess; | ||
| 28 | } | ||
| 29 | |||
| 30 | // Funzione di confronto per l'ordinamento dei tempi (da usare con qsort) | ||
| 31 | int compare_doubles(const void *a, const void *b) { | ||
| 32 | double arg1 = *(const double *)a; | ||
| 33 | double arg2 = *(const double *)b; | ||
| 34 | if (arg1 < arg2) return -1; | ||
| 35 | if (arg1 > arg2) return 1; | ||
| 36 | return 0; | ||
| 37 | } | ||
| 38 | |||
| 19 | // Funzione di timer per un singolo scramble | 39 | // 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) { | 40 | static double timerun_single_scramble(void (*run)(struct ScrambleArgs *, char *, int *), struct ScrambleArgs *args, char *solution, int *solution_length) { |
| 21 | struct timespec start, end; | 41 | struct timespec start, end; |
| @@ -64,50 +84,76 @@ void run_scramble(struct ScrambleArgs *args, char *solution, int *solution_lengt | |||
| 64 | } | 84 | } |
| 65 | 85 | ||
| 66 | void run_all_scrambles(void) { | 86 | void run_all_scrambles(void) { |
| 67 | double total_time = 0.0, average_time = 0.0; | 87 | double total_time = 0.0, average_time = 0.0, variance = 0.0, variance_sum = 0.0, std_deviation = 0.0, median = 0.0; |
| 68 | 88 | ||
| 69 | printf("Solved the following scrambles with timings:\n\n"); | 89 | printf("Solved the following scrambles with timings:\n\n"); |
| 70 | 90 | ||
| 71 | FILE *output_file = fopen("./tools/401_benchmark_performance/compressed_results.txt", "w"); | 91 | FILE *output_file = fopen("./tools/401_benchmark_performance/compressed_results.txt", "w"); |
| 72 | if (!output_file) { | 92 | if (!output_file) { |
| 73 | printf("Error: Could not open output file for writing\n"); | 93 | printf("Error: Could not open output file for writing\n"); |
| 74 | return; | 94 | return; |
| 75 | } | 95 | } |
| 76 | 96 | ||
| 77 | int i; | 97 | int i; |
| 78 | for (i = 0; scrambles[i] != NULL; i++) { | 98 | double scramble_times[MAX_SCRAMBLES]; // Array per memorizzare i tempi per ogni scramble |
| 79 | struct ScrambleArgs args; | ||
| 80 | args.scramble_idx = i; | ||
| 81 | 99 | ||
| 82 | char solution[SOL_BUFFER_LEN]; | 100 | for (i = 0; scrambles[i] != NULL; i++) { |
| 83 | int solution_length = 0; | 101 | struct ScrambleArgs args; |
| 102 | args.scramble_idx = i; | ||
| 84 | 103 | ||
| 85 | double scramble_time = timerun_single_scramble(run_scramble, &args, solution, &solution_length); | 104 | char solution[SOL_BUFFER_LEN]; |
| 86 | total_time += scramble_time; | 105 | int solution_length = 0; |
| 87 | 106 | ||
| 88 | fprintf(output_file, "%d. %s\n", i + 1, scrambles[i]); | 107 | double scramble_time = timerun_single_scramble(run_scramble, &args, solution, &solution_length); |
| 89 | fprintf(output_file, "Solution: %s\n", solution); | 108 | scramble_times[i] = scramble_time; // Salva il tempo per questo scramble |
| 90 | fprintf(output_file, "Length of solution: %d\n", solution_length); | 109 | total_time += scramble_time; |
| 91 | fprintf(output_file, "Time for scramble %d: %.4fs\n", i + 1, scramble_time); | ||
| 92 | fprintf(output_file, "-------------------------\n"); | ||
| 93 | 110 | ||
| 94 | printf("Time for scramble %d: %.4fs\n", i + 1, scramble_time); | 111 | fprintf(output_file, "%d. %s\n", i + 1, scrambles[i]); |
| 95 | printf("Lenght: %d\n", solution_length); | 112 | fprintf(output_file, "Solution: %s\n", solution); |
| 96 | } | 113 | fprintf(output_file, "Length of solution: %d\n", solution_length); |
| 114 | fprintf(output_file, "Time for scramble %d: %.4fs\n", i + 1, scramble_time); | ||
| 115 | fprintf(output_file, "-------------------------\n"); | ||
| 97 | 116 | ||
| 98 | if (i > 0) { | 117 | printf("Time for scramble %d: %.4fs\n", i + 1, scramble_time); |
| 99 | average_time = total_time / i; | 118 | printf("Length: %d\n", solution_length); |
| 100 | } | 119 | } |
| 120 | |||
| 121 | if (i > 0) { | ||
| 122 | average_time = total_time / i; | ||
| 123 | |||
| 124 | // Calcolo della varianza | ||
| 125 | for (int j = 0; j < i; j++) { | ||
| 126 | variance_sum += (scramble_times[j] - average_time) * (scramble_times[j] - average_time); | ||
| 127 | } | ||
| 128 | variance = variance_sum / i; | ||
| 129 | |||
| 130 | // Calcolo della deviazione standard | ||
| 131 | std_deviation = sqrt_approx(variance); // Usa sqrt_approx invece di sqrt() | ||
| 132 | |||
| 133 | // Ordina i tempi per calcolare la mediana | ||
| 134 | qsort(scramble_times, i, sizeof(double), compare_doubles); | ||
| 135 | |||
| 136 | // Calcolo della mediana | ||
| 137 | if (i % 2 == 0) { | ||
| 138 | median = (scramble_times[i / 2 - 1] + scramble_times[i / 2]) / 2.0; | ||
| 139 | } else { | ||
| 140 | median = scramble_times[i / 2]; | ||
| 141 | } | ||
| 142 | } | ||
| 101 | 143 | ||
| 102 | printf("---------\n"); | 144 | printf("---------\n"); |
| 103 | printf("Total time for all scrambles: %.4fs\n", total_time); | 145 | printf("Total time for all scrambles: %.4fs\n", total_time); |
| 104 | printf("Average time per scramble: %.4fs\n", average_time); | 146 | printf("Average time per scramble: %.4fs\n", average_time); |
| 147 | printf("Median time: %.4fs\n", median); // Stampa della mediana | ||
| 148 | printf("Standard deviation of times: %.4fs\n", std_deviation); // Stampa della deviazione standard | ||
| 105 | 149 | ||
| 106 | fprintf(output_file, "---------\n"); | 150 | fprintf(output_file, "---------\n"); |
| 107 | fprintf(output_file, "Total time for all scrambles: %.4fs\n", total_time); | 151 | fprintf(output_file, "Total time for all scrambles: %.4fs\n", total_time); |
| 108 | fprintf(output_file, "Average time per scramble: %.4fs\n", average_time); | 152 | fprintf(output_file, "Average time per scramble: %.4fs\n", average_time); |
| 153 | fprintf(output_file, "Median time: %.4fs\n", median); // Scrivi la mediana nel file | ||
| 154 | fprintf(output_file, "Standard deviation of times: %.4fs\n", std_deviation); // Scrivi la deviazione standard nel file | ||
| 109 | 155 | ||
| 110 | fclose(output_file); | 156 | fclose(output_file); |
| 111 | } | 157 | } |
| 112 | 158 | ||
| 113 | int read_scrambles_from_file(const char *filename, char **scrambles) { | 159 | int read_scrambles_from_file(const char *filename, char **scrambles) { |
