-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
76 lines (60 loc) · 2.1 KB
/
Copy pathmain.cpp
File metadata and controls
76 lines (60 loc) · 2.1 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
/*
* Original function code sourced from Dr. Carlos Arias - Recursive vs. Iterative HW Assignment
* Use of chrono library derived from https://www.geeksforgeeks.org/measure-execution-time-function-cpp/
*
* By: Andrew Macpherson
* 1/16/2023
* */
#include <iostream>
#include <chrono> //Used to measure runtimes
#include <fstream> //Used to output file
using namespace std::chrono;
double iterativePower(double base, int exponent){
double retVal = 1.0;
if (exponent < 0){
return 1.0 / iterativePower(base, -exponent);
}else{
for (int i=0; i<exponent; i++)
retVal *= base;
}
return retVal;
}
double recursivePower(double base, int exponent){
if (exponent < 0){
return 1.0 / recursivePower(base, -exponent);
}else if (exponent == 0){
return 1.0;
}else {
return base * recursivePower(base, exponent - 1);
}
}
int main() {
double base = 3.14159265359;
//Maximum value of n
int max = 165000;
std::ofstream data;
data.open("data.csv");
//CSV Headers
data << "n," << "Iterative Runtime (ms)," << "Recursive Runtime (ms)\n";
//Measures n as many times as possible starting at 1
for (int n = 1; n < max+1; n++) {
//Measures runtime of iterativePower function
auto start = high_resolution_clock::now();
iterativePower(base, n);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
//Saves data to CSV
data << n << "," << duration.count();
//std::cout << "n: " << n << "\tIterative: " << duration.count() << " ms\t"; //Output statement
//Measures runtime of recursivePower function
start = high_resolution_clock::now();
recursivePower(base, n);
stop = high_resolution_clock::now();
duration = duration_cast<microseconds>(stop - start);
//Saves data to CSV
data << "," << duration.count() << std::endl;
//std::cout << "Recursive: " << duration.count() << " ms\n"; //Output statement
}
data.close();
return 0;
}