-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime_functions_in_R.R
279 lines (267 loc) · 6.99 KB
/
runtime_functions_in_R.R
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# calling the C++ functions
Rcpp::sourceCpp("Rcpp_Code.cpp")
# function to calculate required time to sort a particular input array using a user defined cutoff
single_hybrid_runtime <-
function(array_to_be_sorted, cutoff_to_be_used)
{
system.time(sorting_R(array_to_be_sorted, 1, cutoff_to_be_used))["user.self"]
}
# function to calculate the time required for sorting arrays of some particular size using different choices of cutoff
comparative_hybrid_runtime <- function(array_size, cutoff)
{
simulated_array <- rnorm(array_size)
sapply(cutoff, single_hybrid_runtime, array_to_be_sorted = simulated_array)
}
# function to calculate average runtime for user defined array size for different choices of cutoff,
# average being taken over different replications (optionally user defined)
average_hybrid_runtime <-
function(array_size, cutoff, replication = 50)
{
times <-
rowMeans(replicate(
replication,
comparative_hybrid_runtime(array_size, cutoff)
))
names(times) <- cutoff
return(times)
}
# choices of cut-off used for simulation study
keys <- seq(1, 1000, 1)
# simulated average run-times
times_1_e_5 <-
average_hybrid_runtime(array_size = 1e+5, cutoff = keys)
times_2_e_5 <-
average_hybrid_runtime(array_size = 2e+5, cutoff = keys)
times_3_e_5 <-
average_hybrid_runtime(array_size = 3e+5, cutoff = keys)
times_4_e_5 <-
average_hybrid_runtime(array_size = 4e+5, cutoff = keys)
times_5_e_5 <-
average_hybrid_runtime(array_size = 5e+5, cutoff = keys)
times_6_e_5 <-
average_hybrid_runtime(array_size = 6e+5, cutoff = keys)
times_7_e_5 <-
average_hybrid_runtime(array_size = 7e+5, cutoff = keys)
times_8_e_5 <-
average_hybrid_runtime(array_size = 8e+5, cutoff = keys)
times_9_e_5 <-
average_hybrid_runtime(array_size = 9e+5, cutoff = keys)
times_1_e_6 <-
average_hybrid_runtime(array_size = 1e+6, cutoff = keys)
times <-
cbind(
times_1_e_5,
times_2_e_5,
times_3_e_5,
times_4_e_5,
times_5_e_5,
times_6_e_5,
times_7_e_5,
times_8_e_5,
times_9_e_5,
times_1_e_6
)
write.csv(cbind(keys, times), "results.csv", row.names = FALSE)
# performing lowess to suggest the optimum
optimum_cutoff_1_e_5 <-
with(lowess(keys, times_1_e_5, f = 1 / 3), x[which.min(y)])
optimum_cutoff_2_e_5 <-
with(lowess(keys, times_2_e_5, f = 1 / 3), x[which.min(y)])
optimum_cutoff_3_e_5 <-
with(lowess(keys, times_3_e_5, f = 1 / 3), x[which.min(y)])
optimum_cutoff_4_e_5 <-
with(lowess(keys, times_4_e_5, f = 1 / 3), x[which.min(y)])
optimum_cutoff_5_e_5 <-
with(lowess(keys, times_5_e_5, f = 1 / 3), x[which.min(y)])
optimum_cutoff_6_e_5 <-
with(lowess(keys, times_6_e_5, f = 1 / 3), x[which.min(y)])
optimum_cutoff_7_e_5 <-
with(lowess(keys, times_7_e_5, f = 1 / 3), x[which.min(y)])
optimum_cutoff_8_e_5 <-
with(lowess(keys, times_8_e_5, f = 1 / 3), x[which.min(y)])
optimum_cutoff_9_e_5 <-
with(lowess(keys, times_9_e_5, f = 1 / 3), x[which.min(y)])
optimum_cutoff_1_e_6 <-
with(lowess(keys, times_1_e_6, f = 1 / 3), x[which.min(y)])
optimum_cutoffs <-
c(
optimum_cutoff_1_e_5,
optimum_cutoff_2_e_5,
optimum_cutoff_3_e_5,
optimum_cutoff_4_e_5,
optimum_cutoff_5_e_5,
optimum_cutoff_6_e_5,
optimum_cutoff_7_e_5,
optimum_cutoff_8_e_5,
optimum_cutoff_9_e_5,
optimum_cutoff_1_e_6
)
write.table(
t(optimum_cutoffs),
"results.csv",
append = TRUE,
col.names = FALSE,
row.names = "Optimum",
sep = ","
)
lowess_results <- cbind((1:10) * 1e+5, optimum_cutoffs)
colnames(a) <- c("Array Sizes", "Optimum Cut-offs")
# plot of simulated average run-times
png("Plot 1.png")
plot(
keys,
times_1_e_5,
type = "o",
main = "For array size 1e+05",
xlab = "Cutoff Used",
ylab = "Time Taken"
)
lines(lowess(keys, times_1_e_5, f = 1 / 3),
col = "red",
lwd = 2)
dev.off()
png("Plot 2.png")
plot(
keys,
times_2_e_5,
type = "o",
main = "For array size 2e+05",
xlab = "Cutoff Used",
ylab = "Time Taken"
)
lines(lowess(keys, times_2_e_5, f = 1 / 3),
col = "red",
lwd = 2)
dev.off()
png("Plot 3.png")
plot(
keys,
times_3_e_5,
type = "o",
main = "For array size 3e+05",
xlab = "Cutoff Used",
ylab = "Time Taken"
)
lines(lowess(keys, times_3_e_5, f = 1 / 3),
col = "red",
lwd = 2)
dev.off()
png("Plot 4.png")
plot(
keys,
times_4_e_5,
type = "o",
main = "For array size 4e+05",
xlab = "Cutoff Used",
ylab = "Time Taken"
)
lines(lowess(keys, times_4_e_5, f = 1 / 3),
col = "red",
lwd = 2)
dev.off()
png("Plot 5.png")
plot(
keys,
times_5_e_5,
type = "o",
main = "For array size 5e+05",
xlab = "Cutoff Used",
ylab = "Time Taken"
)
lines(lowess(keys, times_5_e_5, f = 1 / 3),
col = "red",
lwd = 2)
dev.off()
png("Plot 6.png")
plot(
keys,
times_6_e_5,
type = "o",
main = "For array size 6e+05",
xlab = "Cutoff Used",
ylab = "Time Taken"
)
lines(lowess(keys, times_6_e_5, f = 1 / 3),
col = "red",
lwd = 2)
dev.off()
png("Plot 7.png")
plot(
keys,
times_7_e_5,
type = "o",
main = "For array size 7e+05",
xlab = "Cutoff Used",
ylab = "Time Taken"
)
lines(lowess(keys, times_7_e_5, f = 1 / 3),
col = "red",
lwd = 2)
dev.off()
png("Plot 8.png")
plot(
keys,
times_8_e_5,
type = "o",
main = "For array size 8e+05",
xlab = "Cutoff Used",
ylab = "Time Taken"
)
lines(lowess(keys, times_8_e_5, f = 1 / 3),
col = "red",
lwd = 2)
dev.off()
png("Plot 9.png")
plot(
keys,
times_9_e_5,
type = "o",
main = "For array size 9e+05",
xlab = "Cutoff Used",
ylab = "Time Taken"
)
lines(lowess(keys, times_9_e_5, f = 1 / 3),
col = "red",
lwd = 2)
dev.off()
png("Plot 10.png")
plot(
keys,
times_1_e_6,
type = "o",
main = "For array size 1e+06",
xlab = "Cutoff Used",
ylab = "Time Taken"
)
lines(lowess(keys, times_1_e_6, f = 1 / 3),
col = "red",
lwd = 2)
dev.off()
# function to compute improvement in runtime for a single replication for a user defined input size
single_improvement <- function(array_size)
{
x <- rnorm(array_size)
hybrid_time <- system.time(sorting_R(x, 1))["user.self"]
quick_time <- system.time(sorting_R(x, 4))["user.self"]
(quick_time - hybrid_time) * 100 / quick_time
}
# function to compute average improvement over multiple replications
average_improvement <- function(length_of_array, replication = 50)
{
mean(replicate(replication, single_improvement(length_of_array)))
}
# simulated sizes used for improvement calculation
sizes <- seq(1e+5, 1e+7, 1e+5)
# simulated percentage improvement data
improvement <- sapply(sizes, average_improvement)
# plot of simulated percentage improvement data
png("Plot 11.png")
plot(
sizes,
improvement,
type = "o",
xlab = "Array Size",
ylab = "Percentage Improvement",
main = "Improvement in Hybrid algorithm over Quick"
)
dev.off()