forked from ylunalin/am205_compiled_lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
speed_test.cc
45 lines (42 loc) · 1.41 KB
/
speed_test.cc
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
36
37
38
39
40
41
42
43
44
45
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;
// This example isn't particularly smartly written
// I have a plain old double for loop to do a dot product of a matrix and a vector
// But the magic is in how compiler can recognize bits of the code that can be optimize
// And do the work for us (sometimes) to improve performance
// Try compiling with optimization flag -O[...]
// The options can be -O, -O1, -O2, -O3, -Ofast (a bit racy)
// Try the following, and compare with python np.dot
// g++ -O3 -o test speed_test.cc
int main() {
int N = 10000;
int S = 100;
clock_t t = clock();
double ** A = new double * [N];
double * b = new double [N];
double * y = new double [N];
for(int i=0;i<N;i++) {
b[i] = rand();
A[i] = new double [N];
for(int j = 0;j<N;j++){
A[i][j] = rand();
}
}
t = clock() - t;
printf("We use %f seconds for initialization\n", (float)t/CLOCKS_PER_SEC);
t = clock();
// Repeat for S times
for(int s = 0; s < S; s++) {
// This double for loop is the equivalent of np.dot (A, b)
for(int i=0;i<N;i++) {
y[i] = 0;
for(int j=0;j<N;j++) {
y[i] += A[i][j] * b[j];
}
}
}
t = clock() - t;
printf("Matrix vector dot product of size %d, repeated %d times, took %8.7f seconds\n", N, S, (float)t/CLOCKS_PER_SEC);
}