-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.cpp
71 lines (53 loc) · 1.64 KB
/
main.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <time.h> // for timespec and clock_gettime()
#include <math.h> // for sqrt, used in cos_sim.h
#include <fstream> // for file read
#include <string> // for std::stod, std::string
#include "cos_sim.h"
#define BILLION 1000000000.0
void vector_from_file(int size, double *vect, char *filename) {
int i;
std::string line;
std::fstream file;
file.open(filename, std::ios::in);
if (!file.is_open()) {
printf("error reading [%s]\n", filename);
perror(NULL);
exit(EXIT_FAILURE);
}
i = 0;
while (getline(file, line)) {
vect[i] = std::stod(line);
i++;
if (i>size) {
break;
}
}
file.close();
if (i!=size) {
printf("error: size [%d] does not match [%d] lines read from [%s]\n", size, i, filename);
exit(EXIT_FAILURE);
}
}
int main(int argc, char *argv[]) {
int size, repeat, i;
double *vector_a, *vector_b;
double avg_runtime, similarity;
struct timespec start, end;
repeat = std::atoi(argv[1]);
size = std::atoi(argv[2]);
vector_a = (double *) malloc(size * sizeof(*vector_a));
vector_b = (double *) malloc(size * sizeof(*vector_a));
vector_from_file(size, vector_a, argv[3]);
vector_from_file(size, vector_b, argv[4]);
avg_runtime = 0;
for(i=0; i<repeat; i++) {
clock_gettime(CLOCK_REALTIME, &start);
Cosine_Similarity sim (vector_a, vector_b, size);
similarity = sim.value;
clock_gettime(CLOCK_REALTIME, &end);
avg_runtime += (end.tv_sec - start.tv_sec) + ((end.tv_nsec - start.tv_nsec)/BILLION);
}
avg_runtime /= repeat;
printf("%.20f %.15f", similarity, avg_runtime);
return 0;
}