-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathmatrix.c
More file actions
35 lines (32 loc) · 891 Bytes
/
matrix.c
File metadata and controls
35 lines (32 loc) · 891 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#define n 1024
double A[n][n];
double B[n][n];
double C[n][n];
float tdiff(struct timeval *start, struct timeval *end) {
return (end->tv_sec - start->tv_sec) +
1e-6 *(end->tv_usec - start->tv_usec);
}
int main(int argc, const char *argv[]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = (double)rand() / (double)RAND_MAX;
B[i][j] = (double)rand() / (double)RAND_MAX;
C[i][j] = 0;
}
}
struct timeval start, end;
gettimeofday(&start, NULL);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
gettimeofday(&end, NULL);
printf("%0.6f\n", tdiff(&start, &end));
return 0;
}