-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLRUComparison.cs
232 lines (182 loc) · 5.23 KB
/
LRUComparison.cs
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using System;
using System.Collections;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
int frames = 1;
// Generate single reference string length 500,000, each value ranging from 0 to 20
int[] arr = new int[500000];
InitArray(arr, 500000, 0, 21);
// Generating test cases for frame counts between 1 and 10
for (; frames <= 10; frames++)
{
// Establish stopwatch variable to record time spent in first algorithm
var w1 = System.Diagnostics.Stopwatch.StartNew();
int pf1 = LeastRecentlyUsedCounter(arr, frames);
w1.Stop();
// Store the elapsed milliseconds for first algorithm
long runtimeLRUCounter = w1.ElapsedMilliseconds;
// Establish stopwatch variable to record time spent in second algorithm
var w2 = System.Diagnostics.Stopwatch.StartNew();
int pf2 = LeastRecentlyUsedStack(arr, frames);
w2.Stop();
// Store the elapsed milliseconds for second algorithm
long runtimeLRUStack = w2.ElapsedMilliseconds;
Console.WriteLine("Running time for " + frames + " frames: ");
Console.WriteLine("Using Counter implementation: " + runtimeLRUCounter + " milliseconds");
Console.WriteLine("Using Counter implementation: " + pf1 + " page faults\n");
Console.WriteLine("Using Stack implementation: " + runtimeLRUStack + " milliseconds");
Console.WriteLine("Using Stack implementation: " + pf2 + " page faults\n");
Console.WriteLine("");
}
}
static void InitArray(int[] array, int values, int l, int r)
{
Random random = new Random();
for (int i = 0; i < values; i++)
{
array[i] = random.Next(l, r);
}
}
static int LeastRecentlyUsedCounter(int[] refStr, int frames)
{
int totalPageFault = 0;
int[] mem = new int[frames];
int[,] lookup = new int[21, 2];
for (int i = 0; i < mem.Length; i++)
{
mem[i] = -1;
}
List<int> values = new List<int>();
for (int i = 0; i < refStr.Length; i++)
{
Boolean alreadyPresent = false;
// Value to be inserted in the memory
int insert = refStr[i];
// Determine if value to be inserted is already present
for (int a = 0; a < mem.Length; a++)
{
if (mem[a] == insert)
{
alreadyPresent = true;
lookup[insert, 1] = 0;
break;
}
}
if (!alreadyPresent)
{
totalPageFault++;
int index = IsEmptySpace(mem);
// Case #1: Empty space present in memory block
if (index != -1)
{
mem[index] = insert;
values.Add(insert);
lookup[insert, 1] = 0;
}
// Case #2: Value in memory block must be replaced
else
{
int max = -1;
int min = refStr.Length + 2;
int indy = 0;
// Cycle through values in memory block
for (int j = 0; j < mem.Length; j++)
{
// Determine how many times value has been used
int used = lookup[mem[j], 0];
// Determine how many steps it's been since value last used
int steps = lookup[mem[j], 1];
if (steps > max)
{
min = used;
max = steps;
indy = j;
}
}
values.Remove(mem[indy]);
mem[indy] = insert;
values.Add(insert);
lookup[insert, 0]++;
lookup[insert, 1] = 0;
}
}
foreach (int key in values)
{
lookup[key, 1]++;
}
}
return totalPageFault;
}
static int LeastRecentlyUsedStack(int[] refStr, int frames)
{
int totalPageFault = 0;
LinkedList<int> vals = new LinkedList<int>();
int[] mem = new int[frames];
for (int i = 0; i < mem.Length; i++)
{
mem[i] = -1;
}
for (int i = 0; i < refStr.Length; i++)
{
Boolean alreadyPresent = false;
int insert = refStr[i];
// If the referenced insert value is not already in the list
if (!vals.Contains(insert))
{
// Add it to the top of the list
vals.AddFirst(insert);
} else
// Otherwise, the value needs to be plucked out and placed on top
{
vals.Remove(insert);
vals.AddFirst(insert);
}
for (int a = 0; a < mem.Length; a++)
{
if (mem[a] == insert)
{
alreadyPresent = true;
break;
}
}
if (!alreadyPresent)
{
totalPageFault++;
int index = IsEmptySpace(mem);
// Case #1: Empty space present in memory block
if (index != -1)
{
mem[index] = insert;
}
else {
int toBeRemoved = vals.Last.Value;
vals.RemoveLast();
for (int m = 0; m < mem.Length; m++)
{
if (mem[m] == toBeRemoved)
{
mem[m] = insert;
break;
}
}
}
}
}
return totalPageFault;
}
// Method used to see if there exists empty space in the memory block
static int IsEmptySpace(int[] array)
{
for (int i = 0; i < array.Length; i++)
{
if (array[i] == -1)
{
return i;
}
}
return -1;
}
}