This repository has been archived by the owner on Oct 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PriorityQueueBigOh.cpp
102 lines (81 loc) · 2.55 KB
/
PriorityQueueBigOh.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Lab 12b, Perform A Simple Timing Study
// Programmer: Minos Park
// Editor(s) used: Sublime Text 2
// Compiler(s) used: G++
#include <iostream>
#include <string>
using namespace std;
#include <cassert>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include "PriorityQueue.h"
int main()
{
// print my name and this assignment's title
cout << "Lab 12b, Perform A Simple Timing Study\n";
cout << "Programmer: Minos Park\n";
cout << "Editor(s) used: Sublime Text 2\n";
cout << "Compiler(s) used: G++\n";
cout << "File: " << __FILE__ << endl;
cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;
int tempInt;
int N = 5000000;
const int REPS = 70000;
for(int select = 1; select < 3; select++)
{
int n = N;
int elapsedTimeTicksNorm = 0;
double expectedTimeTicks = 0;
if(select == 1)
cout << "Enqueue, O(log n)" << endl;
if(select == 2)
cout << "Dequeue, O(log n)" << endl;
for(int cycle = 0; cycle < 4; cycle++, n *= 2)
{
PriorityQueue<double> l(2 * n + REPS);
for(int i = n; i > 0; i--)
l.enqueue(i);
assert(l.size() == n);
string bigOh = "O(log n)";
clock_t startTime = clock();
if(select == 1)
{
for(int r = n+1; r < n+REPS; r++)
l.enqueue(r);
}
if(select == 2)
{
for(int r = 0; r < REPS; r++)
l.dequeue();
}
clock_t endTime = clock();
long elapsedTimeTicks = (long)(endTime - startTime);
double factor = pow(2.0, cycle);
if (select == 1)
{
for (int i = n+REPS-1; i > 0; i--)
assert(l.dequeue() == i);
}
if(cycle == 0)
elapsedTimeTicksNorm = elapsedTimeTicks;
else if(bigOh == "O(1)")
expectedTimeTicks = elapsedTimeTicksNorm;
else if(bigOh == "O(log n)")
expectedTimeTicks = log(double(n)) / log(n / factor) * elapsedTimeTicksNorm;
else if(bigOh == "O(n)")
expectedTimeTicks = factor * elapsedTimeTicksNorm;
else if(bigOh == "O(n log n)")
expectedTimeTicks = factor * log(double(n)) / log(n / factor) * elapsedTimeTicksNorm;
else if(bigOh == "O(n squared)")
expectedTimeTicks = factor * factor * elapsedTimeTicksNorm;
cout << " " << elapsedTimeTicks;;
if(cycle == 0)
cout << " (expected " << bigOh << ')';
else
cout << " (expected " << expectedTimeTicks << ')';
cout << " for n=" << n << endl;
}
cout << endl;
}
}