Skip to content

Instantly share code, notes, and snippets.

@karngyan
Created July 12, 2020 00:20
Show Gist options
  • Save karngyan/013b59283fe9af6bc8c755bf4d698c2b to your computer and use it in GitHub Desktop.
Save karngyan/013b59283fe9af6bc8c755bf4d698c2b to your computer and use it in GitHub Desktop.
#include<stdlib.h>
#include<stdio.h>
#include<sys/time.h>
#define n 4096
double A[n][n];
double B[n][n];
double C[n][n];
float time_difference(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 seconds\n", time_difference(&start, &end));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment