diff options
author | Lucas de Castro Borges <lucas@gnuabordo.com.br> | 2024-04-22 00:35:56 -0300 |
---|---|---|
committer | Lucas de Castro Borges <lucas@gnuabordo.com.br> | 2024-04-22 00:35:56 -0300 |
commit | 754b5d1114f096778e483f8a6f3a5dc333225e26 (patch) | |
tree | 30911ec9da4cfd2f5572c27f7288fcbfa4cd212d /modules/benchmark/nqueens.c | |
parent | 35c2857da302ab8b3c308052f2cd1674fb4141a6 (diff) | |
parent | 5f01c706267c595de92406a32e7f31ef5056c2d0 (diff) |
Update upstream source from tag 'upstream/2.0.3pre'
Update to upstream version '2.0.3pre'
with Debian dir 6683980bf6b5c02f6847fd56765833301f75f4f3
Diffstat (limited to 'modules/benchmark/nqueens.c')
-rw-r--r-- | modules/benchmark/nqueens.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/modules/benchmark/nqueens.c b/modules/benchmark/nqueens.c new file mode 100644 index 00000000..6aad7638 --- /dev/null +++ b/modules/benchmark/nqueens.c @@ -0,0 +1,68 @@ +/* + * N-Queens Problem Solver + * Found somewhere on the Internet; can't remember where. Possibly Wikipedia. + */ +#include <stdio.h> +#include <stdbool.h> +#include <stdlib.h> + +#include "hardinfo.h" +#include "benchmark.h" + +/* if anything changes in this block, increment revision */ +#define BENCH_REVISION 2 +#define QUEENS 6 +#define CRUNCH_TIME 5 + +int row[QUEENS]; + +bool safe(int x, int y) { + int i; + for (i = 1; i <= y; i++) + if (row[y - i] == x || row[y - i] == x - i || row[y - i] == x + i) + return false; + return true; +} + +int nqueens(int y) { + int x; + + for (x = 0; x < QUEENS; x++) { + if (safe((row[y - 1] = x), y - 1)) { + if (y < QUEENS) { + nqueens(y + 1); + } else { + break; + } + } + } + + return 0; +} + +static gpointer nqueens_for(void *data, gint thread_number) +{ + nqueens(0); + + return NULL; +} + +void +benchmark_nqueens(void) +{ + bench_value r = EMPTY_BENCH_VALUE; + + shell_view_set_enabled(FALSE); + shell_status_update("Running N-Queens benchmark..."); + + r = benchmark_crunch_for(CRUNCH_TIME, 0, nqueens_for, NULL); + + r.revision = BENCH_REVISION; + snprintf(r.extra, 255, "q:%d", QUEENS); + + r.result /= 25; + + bench_results[BENCHMARK_NQUEENS] = r; +} + + |