#include #include #include #include #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() */ /* TODO: Add timing. For now the macro executes the given function once */ 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); }