Created
January 9, 2021 20:55
-
-
Save PlushBeaver/6c0903cc8432fe2e9dfe2cbfdd51b4e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* gcc -O2 matrix.c -o matrix | |
* | |
* for i in {1..30}; do echo 768 | ./matrix; done > matrix.csv | |
* | |
* R | |
* data = read.csv("matrix.csv", header = FALSE) | |
* wilcox.test(data$V1, data$V2, alternative = "two.sided") | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
void | |
multiply_static(double *a, double *b, double *c, size_t n) | |
{ | |
static const size_t SIZE = 768; | |
size_t i, j, k; | |
(void)n; | |
for (i = 0; i < SIZE; i++) | |
for (j = 0; j < SIZE; j++) | |
for (k = 0; k < SIZE; k++) | |
c[i * SIZE + j] = a[i * SIZE + k] * b[k * SIZE + j]; | |
} | |
void | |
multiply_dynamic(double *a, double *b, double *c, size_t n) | |
{ | |
size_t i, j, k; | |
for (i = 0; i < n; i++) | |
for (j = 0; j < n; j++) | |
for (k = 0; k < n; k++) | |
c[i * n + j] = a[i * n + k] * b[k * n + j]; | |
} | |
void | |
fill(double *a, size_t n) | |
{ | |
size_t i, j; | |
for (i = 0; i < n; i++) | |
for (j = 0; j < n; j++) | |
a[i * n + j] = (double)rand() / RAND_MAX; | |
} | |
typedef void (*func_type)(double *a, double *b, double *c, size_t n); | |
double | |
test(func_type func, size_t n) | |
{ | |
double *a, *b, *c; | |
clock_t t0, t1; | |
a = calloc(n * n, sizeof(*a)); | |
b = calloc(n * n, sizeof(*b)); | |
c = calloc(n * n, sizeof(*c)); | |
if (!a || !b || !c) { | |
perror("malloc"); | |
exit(EXIT_FAILURE); | |
} | |
t0 = clock(); | |
func(a, b, c, n); | |
t1 = clock(); | |
return (double)(t1 - t0) / CLOCKS_PER_SEC; | |
} | |
int | |
main(int argc, char **argv) | |
{ | |
size_t n; | |
double dt, st; | |
scanf("%zu", &n); | |
dt = test(multiply_dynamic, n); | |
st = test(multiply_static, n); | |
printf("%f,%f\n", dt, st); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment