-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_old.cpp
More file actions
116 lines (92 loc) · 2.94 KB
/
main_old.cpp
File metadata and controls
116 lines (92 loc) · 2.94 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include "fraction.h"
#include <sys/time.h>
#include <sys/resource.h>
#include <time.h>
using namespace std;
int genRand(int max) {
return ((double)rand() / (double)RAND_MAX) * max + 1;
}
double iterate(char op, int64_t iters, const fraction &f1, const fraction &f2) {
struct timeval timer1, timer2;
gettimeofday(&timer1, NULL);
switch (op) {
case '*':
for(int64_t i = 0; i < iters; i++)
f1 * f2;
break;
case '-':
for(int64_t i = 0; i < iters; i++)
f1 - f2;
break;
case '/':
for(int64_t i = 0; i < iters; i++)
f1 / f2;
break;
case '+':
for(int64_t i = 0; i < iters; i++)
f1 + f2;
break;
default:
fprintf(stderr, "op %c not recognized\n", op);
}
gettimeofday(&timer2, NULL);
int64_t elapsedSeconds = timer2.tv_sec - timer1.tv_sec;
int64_t elapsedUSeconds = timer2.tv_usec - timer1.tv_usec;
return (elapsedSeconds + elapsedUSeconds / 1000000.0);
}
void timeIt() {
struct timeval timer1, timer2;
gettimeofday(&timer1, NULL);
for(int i = 0; i < 1000000; i++) {
fraction::getBestRA(-4.152521, 50);
}
gettimeofday(&timer2, NULL);
int64_t elapsedSeconds = timer2.tv_sec - timer1.tv_sec;
int64_t elapsedUSeconds = timer2.tv_usec - timer1.tv_usec;
cout << "Took " << (elapsedSeconds + elapsedUSeconds / 1000000.0) << endl;
}
void timeOperators(int64_t iters, const fraction &f1, const fraction &f2) {
char ops[4] = {'*', '-', '/', '+'};
for(int8_t i = 0; i < 4; i++) {
cout << iters << " iterations of " << ops[i] << " operator";
cout << " took " << iterate(ops[i], iters, f1, f2);
cout << " seconds\n";
}
}
int main(int argc, char *argv[]) {
struct timeval timer1, timer2;
gettimeofday(&timer1, NULL);
srand(42);
fraction a(genRand(100), genRand(100));
fraction b(genRand(100), genRand(100));
fraction f1(2, 6);
fraction f2(4, 12);
cout << "a = " << a << ", b = " << b << endl;
cout << "f1 = " << f1 << ", f2 = " << f2 << endl << endl;
cout << "a + b = " << a + b << endl;
cout << "a - b = " << a - b << endl;
cout << "a * b = " << (a * b).reduce() << endl;
cout << "a / b = " << a / b << endl << endl;
cout << "f1 == f2 = " << (f1 == f2) << endl;
cout << "f1 != f2 = " << (f1 != f2) << endl;
cout << "a > b = " << (a > b) << endl;
cout << "a < b = " << (a < b) << endl;
cout << "b < a = " << (b < a) << endl;
cout << "b > a = " << (b > a) << endl;
cout << "f1 <= f2 = " << (f1 <= f2) << endl;
cout << "f1 >= f2 = " << (f1 >= f2) << endl;
cout << "f2 = " << f2 << endl;
f2.reduce();
timeOperators(argc > 1 ? atoi(argv[1]) : 10000, a, b);
gettimeofday(&timer2, NULL);
int64_t elapsedSeconds = timer2.tv_sec - timer1.tv_sec;
int64_t elapsedUSeconds = timer2.tv_usec - timer1.tv_usec;
cout << "Entire program took " << (elapsedSeconds + elapsedUSeconds / 1000000.0) << endl;
struct rusage my;
getrusage(RUSAGE_SELF, &my);
cout << "Mem = " << my.ru_maxrss << endl;
cout << "Val = " << fraction::getBestRA(-4.152521, 50) << endl;
timeIt();
return 0;
}