blob: b583e84dd93b6c5043ffdaa53f219e7775170c0f (
plain)
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
|
import java.util.Random;
public class DLinkedListTester
{
public static void main(String[] args)
{
// Construct a list and add entries
int numberOfData = 100000; // number of data to add to the list
int maxDataValue = 10000; // number of different values to add to the list
int numSampling = 10000; // number of calling the method getEntry
int dryRunReps = numSampling / 1000;
DLinkedList<Integer> list = new DLinkedList<Integer>();
Random rand = new Random();
rand.setSeed(System.nanoTime());
System.out.print("Adding data into the list: ");
for(int i = 0; i < numberOfData; i++)
{
list.add(rand.nextInt(maxDataValue));
}
System.out.println("DONE");
int[] positions = new int[numSampling];
int[] returnValues = new int[numSampling];
System.out.print("Dry run for the method getEntry(): ");
// Dry run for the method getEntry1 (no timer)
// Also store all result for testing the method getEntry2()
for(int i = 0; i < numSampling; i++)
{
int aPosition = rand.nextInt(numberOfData) + 1;
positions[i] = aPosition;
returnValues[i] = list.getEntry(aPosition);
}
System.out.println("DONE");
double startTime; // Start time millisecond
double endTime; // End time millisecond
System.out.print("Time the method getEntry(): ");
// Timing the method getEntry()
startTime = System.currentTimeMillis();
for(int i = 0; i < numSampling; i++)
{
if(returnValues[i] != list.getEntry(positions[i]))
{
System.out.println("There is something wrong.");
System.out.println("The entry in position " + positions[i] + " should be " + returnValues[i] + ".");
System.out.println("But the method getEntry(" + positions[i] + ") returns " + list.getEntry(positions[i]) + ".");
System.out.println("Fix the method getNodeAt() first.");
return;
}
}
endTime = System.currentTimeMillis();
double getEntryTime = endTime - startTime;
System.out.println("DONE");
// getEntry1() - Solution 1
// Dry run (no timer)
System.out.print("Dry run for the method getEntry1(): ");
for(int i = 0; i < dryRunReps; i++)
{
if(returnValues[i] != list.getEntry1(positions[i]))
{
System.out.println("There is something wrong.");
System.out.println("The entry in position " + positions[i] + " should be " + returnValues[i] + ".");
System.out.println("But the method getEntry1(" + positions[i] + ") returns " + list.getEntry1(positions[i]) + ".");
System.out.println("Fix the method getNodeAt1() first.");
return;
}
}
System.out.println("DONE");
// Timing the method getEntry1()
System.out.print("Time the method getEntry1(): ");
startTime = System.currentTimeMillis();
for(int i = 0; i < numSampling; i++)
{
if(returnValues[i] != list.getEntry1(positions[i]))
{
System.out.println("There is something wrong.");
System.out.println("The entry in position " + positions[i] + " should be " + returnValues[i] + ".");
System.out.println("But the method getEntry1(" + positions[i] + ") returns " + list.getEntry1(positions[i]) + ".");
System.out.println("Fix the method getNodeAt1() first.");
return;
}
}
endTime = System.currentTimeMillis();
double getEntry1Time = endTime - startTime;
System.out.println("DONE");
// getEntry2() - Solution 2
// Dry run (no timer)
System.out.print("Dry run for the method getEntry2(): ");
for(int i = 0; i < dryRunReps; i++)
{
if(returnValues[i] != list.getEntry2(positions[i]))
{
System.out.println("There is something wrong.");
System.out.println("The entry in position " + positions[i] + " should be " + returnValues[i] + ".");
System.out.println("But the method getEntry2(" + positions[i] + ") returns " + list.getEntry2(positions[i]) + ".");
System.out.println("Fix the method getNodeAt2() first.");
return;
}
}
System.out.println("DONE");
// Timing the method getEntry2()
System.out.print("Time the method getEntry2(): ");
startTime = System.currentTimeMillis();
for(int i = 0; i < numSampling; i++)
{
if(returnValues[i] != list.getEntry2(positions[i]))
{
System.out.println("There is something wrong.");
System.out.println("The entry in position " + positions[i] + " should be " + returnValues[i] + ".");
System.out.println("But the method getEntry2(" + positions[i] + ") returns " + list.getEntry2(positions[i]) + ".");
System.out.println("Fix the method getNodeAt2() first.");
return;
}
}
endTime = System.currentTimeMillis();
double getEntry2Time = endTime - startTime;
System.out.println("Done\n");
System.out.println("The method getEntry() took " + getEntryTime + " millisecond.");
System.out.println("The method getEntry1() took " + getEntry1Time + " millisecond.");
System.out.println("The method getEntry2() took " + getEntry2Time + " millisecond.\n");
System.out.println("If getEntry1() took roughly about half the time of getEntry(), you get 5 points.");
System.out.println("If getEntry2() took roughly about a quarter the time of getEntry(), you get 5 points.");
}
}
|