-
Notifications
You must be signed in to change notification settings - Fork 0
/
alg_analysis.cpp
82 lines (70 loc) · 2.63 KB
/
alg_analysis.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
72
73
74
75
76
77
78
79
80
81
82
// alg_analysis.cpp
// Determine runtime of a provided algorithm given specific inputs
// Written by Matthew Sauder
#include <iostream>
#include <ctime>
#include <cmath>
#include <fstream>
using namespace std;
// Use nested for-loop to compute a summation
long long assignment1Algorithm(long long n);
int main() {
clock_t start;
clock_t finish;
long long result;
long long n;
long double timeUsed;
bool continueQ = false;
char c; // placeholder char
ofstream outfile("A1output.csv");
// Initialize output CSV file with appropriate headings
outfile << "\"result\",\"n/result\",\"n*log2(n)/result\",\"n^(3/2)/result\",\"timeUsed\",\"n/timeUsed\",\"n*log2(n)/timeUsed\",\"n^(3/2)/timeUsed\"" << endl;
do {
// Prompt user for an integer to perform the sum on
cout << "Enter an integer: ";
cin >> n;
// Determine runtime by measuring clock cycles between starting and ending the algorithm and then dividing by the # of clocks per second
start = clock();
result = assignment1Algorithm(n);
finish = clock();
timeUsed = ((long double)(finish - start))/CLOCKS_PER_SEC;
// Output results to screen
cout << "result = " << result << endl;
cout << "n/result = " << ((float)n)/result << endl;
cout << "n*log2(n)/result = " << n*log(n)/log(2)/result << endl;
cout << "pow(n, 1.5)/result = " << pow(n, 1.5)/result << endl;
cout << "timeUsed = " << timeUsed << endl;
cout << "n/timeUsed = " << n/timeUsed << endl;
cout << "n*log2(n)/timeUsed = " << n*log(n)/timeUsed/log(2) << endl;
cout << "pow(n, 1.5)/timeUsed = " << pow(n, 1.5)/timeUsed << endl;
// Output results into a CSV file (for analysis later)
outfile << result << ",";
outfile << ((float)n)/result << ",";
outfile << n*log(n)/log(2)/result << ",";
outfile << pow(n, 1.5)/result << ",";
outfile << timeUsed << ",";
outfile << n/timeUsed << ",";
outfile << n*log(n)/(timeUsed*log(2)) << ",";
outfile << pow(n, 1.5)/timeUsed << endl;
cout << "Would you like to continue (Y/N)? ";
cin >> c;
// Exit loop if user does not wish to continue
if (c == 'y' || c == 'Y') {
continueQ = true;
}
else continueQ = false;
} while (continueQ);
return 0;
}
// Use nested for-loop to compute a summation
// Pre: some number of steps to sum over is input by the user
// Post: summation is completed and returned by function
long long assignment1Algorithm(long long n) {
long long sum = 0;
for (long long i = 1; i <= n; i++) {
for (long long j = n; j > 1; j = floor(j/2)) {
sum += 1;
}
}
return sum;
}