blob: ddb82d1f481a869c39cea4d134d2f429cf95a518 (
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
|
/**
*
*/
package de.fhswf.in.inf.java1.aufgabe6;
import java.util.Iterator;
import java.util.Vector;
import de.fhswf.in.inf.java1.aufgabe4.Fill;
/**
* Main function for testing Vectors and Lists.
*
* @author $Author: $
* @version $Revision: $, $Date: $ UTC
*/
public final class Aufgabe6
{
/**
* Prevents instantiation of the utility class.
*
*/
private Aufgabe6()
{
}
/**
* Main function for testing Vectors and Lists.
*
* @param args
* Command line arguments.
*/
public static void main(String[] args)
{
final int testLength = 100000000;
Vector<Integer> test = new Vector<>(testLength);
Integer testGet;
for (int i = 0; i < test.capacity(); i++)
{
test.add(i);
}
long start;
start = System.currentTimeMillis(); // Gets current time in µs
for (int i = 0; i < test.size(); i++)
{
testGet = test.elementAt(i);
}
// Calculates the runtime of for
System.out.println("Elapsed time: "
+ (System.currentTimeMillis() - start) + " ms");
start = System.currentTimeMillis(); // Gets current time in µs
for (Iterator<Integer> it = test.iterator(); it.hasNext();)
{
testGet = it.next();
}
// Calculates the runtime of for
System.out.println("Elapsed time: "
+ (System.currentTimeMillis() - start) + " ms");
start = System.currentTimeMillis(); // Gets current time in µs
for (Integer integer : test)
{
testGet = integer;
}
// Calculates the runtime of foreach
System.out.println("Elapsed time: "
+ (System.currentTimeMillis() - start) + " ms");
}
}
|