summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2023-03-26 18:55:32 +0200
committerMitya Selivanov <automainint@guattari.tech>2023-03-26 18:55:32 +0200
commitb17726fe51a1c7c1eccaef2e90cbab7ee022557f (patch)
tree35592b87b30feacbdf12819b808679d6d4b51057 /source
parent6b911aca700eebcbff4e7369b117731a8f3e2a71 (diff)
downloadkit-b17726fe51a1c7c1eccaef2e90cbab7ee022557f.zip
[benchmarks] DO_NOT_OPTIMIZE macro
Diffstat (limited to 'source')
-rw-r--r--source/kit_test/bench.h9
-rw-r--r--source/test/unittests/foo.bench.c16
2 files changed, 23 insertions, 2 deletions
diff --git a/source/kit_test/bench.h b/source/kit_test/bench.h
index 131d89d..6c701bb 100644
--- a/source/kit_test/bench.h
+++ b/source/kit_test/bench.h
@@ -73,12 +73,21 @@ void kit_bench_register(char const *name, char const *file,
kit_bench_end_fn_(kit_bench_index_); \
}
+/* FIXME
+ */
+#define KIT_DO_NOT_OPTIMIZE(x) \
+ do { \
+ volatile void *bench_ptr_ = &(x); \
+ (void) bench_ptr_; \
+ } while (0)
+
int kit_run_benchmarks(int argc, char **argv);
#ifndef KIT_DISABLE_SHORT_NAMES
# define BENCHMARK KIT_BENCHMARK
# define BENCHMARK_BEGIN KIT_BENCHMARK_BEGIN
# define BENCHMARK_END KIT_BENCHMARK_END
+# define DO_NOT_OPTIMIZE KIT_DO_NOT_OPTIMIZE
# define bench_register kit_bench_register
# define run_benchmarks kit_run_benchmarks
diff --git a/source/test/unittests/foo.bench.c b/source/test/unittests/foo.bench.c
index 531f1d8..a3d5c18 100644
--- a/source/test/unittests/foo.bench.c
+++ b/source/test/unittests/foo.bench.c
@@ -1,11 +1,23 @@
#define KIT_BENCH_FILE foo
#include "../../kit_test/bench.h"
+struct test_foo_ {
+ double f;
+};
+
BENCHMARK("foo") {
BENCHMARK_BEGIN;
{
- volatile int x = 0;
- for (int i = 0; i < 200000; i++) x++;
+ int x = 0;
+ struct test_foo_ f = { 0. };
+
+ for (int i = 0; i < 100000; i++) {
+ x += (1 << 1);
+ x ^= i;
+ f.f += 0.1;
+ }
+ DO_NOT_OPTIMIZE(x);
+ DO_NOT_OPTIMIZE(f);
}
BENCHMARK_END;
}