summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJasper2025-09-01 20:21:25 +0200
committerJasper2025-09-01 20:21:25 +0200
commitabf764dd2d8970150f169b88bd65dadd7baf661f (patch)
tree34f1f31f37cf3de817722cc5bda054de4e8b9ad4
parente4d20a643de1e1e404b74a8b13b76ba62125369d (diff)
Started work on macros to benchmark given functions
-rw-r--r--debug.c98
1 files changed, 97 insertions, 1 deletions
diff --git a/debug.c b/debug.c
index df2071b..844e8e8 100644
--- a/debug.c
+++ b/debug.c
@@ -1,12 +1,108 @@
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
+#include <unistd.h>
#include "tests.h"
#include "matrix.h"
+#define STR_FMT(str, ...) \
+ do { \
+ int tmp = asprintf(&(str), __VA_ARGS__); \
+ if (tmp < 0) str = NULL; \
+ } while (0)
+
+#define TIMER(func, details, samples, ...) \
+ do { \
+ size_t _samples = (samples); \
+ clock_t _t; \
+ double _xs[_samples]; \
+ for (size_t _i = 0; _i < _samples; ++_i) \
+ { \
+ _t = clock(); \
+ (__VA_ARGS__); \
+ _xs[_i] = ((double) (clock() - _t)) / CLOCKS_PER_SEC; \
+ } \
+ double max = _xs[0], min = _xs[0], sum = 0; \
+ \
+ for (size_t i = 0; i < _samples; ++i) \
+ { \
+ if (_xs[i] > max) max = _xs[i]; \
+ else if (_xs[i] < min) min = _xs[i]; \
+ sum += _xs[i]; \
+ } \
+ printf("BENCHMARK (CPU time in s)\n"); \
+ printf("---------------------\n"); \
+ printf("Function : %s\n", (func)); \
+ if ((details)[0] != '\0') \
+ printf("Details : %s\n", (details)); \
+ printf("Total (%ld samples) : %f\n", _samples, sum); \
+ printf("Avg. : %f\n", sum / _samples); \
+ printf("Max : %f\n", max); \
+ printf("Min : %f\n", min); \
+ } while (0)
+
+#define BENCHMARK_RAND1(m, n, bound_l, bound_u, func,...) \
+ do { \
+ Matrix *A = matrix_rand((m), (n), (bound_l), (bound_u), MATRIX_NONE); \
+ func(A, __VA_ARGS__); \
+ free(A); \
+ } while(0)
+
int main()
{
srand(time(NULL));
- run_tests();
+ /* run_tests() */
+
+ /* TODO: Add timing. For now the macro executes the given function once */
+ /* TODO: Fix for functions that take just the matrix and no additional arguments */
+ BENCHMARK_RAND1(
+ 100, 100, /* Dimensions: m x n */
+ 0, 100, /* Value bounds */
+ matrix_swap1, /* Function to be tested */
+ MATRIX_SWAP_ROWS, 0, 1 /* List of arguments to function */
+ );
+
+ Matrix *A, *B;
+ int n = 100;
+ size_t samples = 500;
+ char *str;
+
+ STR_FMT(str, "rows, %dx%d random matrices", n, n);
+
+ TIMER("swap()", str, samples,
+ A = matrix_rand(n, n, 0, 100, MATRIX_NONE),
+ matrix_swap(A, MATRIX_SWAP_ROWS, 0, 1),
+ matrix_free(A)
+ );
+
+ printf("\n");
+
+ TIMER("swap1()", str, samples,
+ A = matrix_rand(n, n, 0, 100, MATRIX_NONE),
+ matrix_swap1(A, MATRIX_SWAP_ROWS, 0, 1),
+ matrix_free(A)
+ );
+
+ printf("\n");
+
+ TIMER("matrix_eq()", "", samples,
+ A = matrix_rand(n, n, 0, 100, MATRIX_NONE),
+ B = matrix_rand(n, n, 0, 100, MATRIX_NONE),
+ matrix_eq(A, B, 0.01),
+ matrix_free(A),
+ matrix_free(B)
+ );
+
+ printf("\n");
+
+ TIMER("matrix_add()", "", samples,
+ A = matrix_rand(n, n, 0, 100, MATRIX_NONE),
+ B = matrix_rand(n, n, 0, 100, MATRIX_NONE),
+ matrix_add(A, B),
+ matrix_free(A),
+ matrix_free(B)
+ );
+
+ free(str);
}