-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGetSetTimes.cpp
More file actions
50 lines (44 loc) · 1.25 KB
/
GetSetTimes.cpp
File metadata and controls
50 lines (44 loc) · 1.25 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
/*
GetSetTimes.cpp
A program to measure get and set costs on ArrayDeque and SLList.
bds 2014Mara
*/
#include <ctime>
#include <iostream>
#include "ArrayDeque.h"
#include "SLList.h"
template<class NumList>
int test(NumList & L, int n);
int main(){
int n = 1000000;
ods::ArrayDeque<float> AD;
std::cout << "Starting ArrayDeque Get/set test " << std::endl;
int ADtime = test(AD, n);
std::cout << "ArrayDeque Get/set time is " << ADtime;
std::cout << " for " << n << " trials." << std::endl << std::endl;
ods::SLList<float> SL;
std::cout << "Starting SLList Get/set test " << std::endl;
int SLtime = test(SL, n);
std::cout << "SLList Get/set time is " << SLtime;
std::cout << " for " << n << " trials." << std::endl << std::endl;
}
template<class NumList>
int test(NumList & L, int n){
// build the list with Stack function push().
for (int i = 0; i < n; ++i)
L.push(0);
// Now view it as a List and record the time of some get and set actions.
int start = clock();
// Set the entries.
for (int i = 0; i < n; ++i)
L.set(i, -i);
// Get and change the entries.
for (int i = 0; i < n; ++i) {
int x = L.get(i);
L.set(i, x+1);
}
// sanity check
int k = n/2;
if (L.get(k) != -k + 1) std::cerr << "get/set error" << std::endl;
return clock()-start;
}