-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculateSortTimeProject.java
More file actions
76 lines (64 loc) · 2.45 KB
/
CalculateSortTimeProject.java
File metadata and controls
76 lines (64 loc) · 2.45 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
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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
import java.util.Vector;
public class CalculateSortTimeProject {
public static void main(String[] args) {
int[] myArray= new int[50000000];
ArrayList<Integer> myList= new ArrayList<Integer>(50000000);
Vector<Integer> myVector= new Vector<Integer>(50000000);
double totalArray= 0;
double totalList= 0;
double totalVector= 0;
double totalSortArray= 0;
double totalSortList= 0;
double totalSortVector= 0;
Random random= new Random();
int[] randArray= new int[50000000];
for (int j=0; j<50000000; j++) {
randArray[j]= random.nextInt(1, Integer.MAX_VALUE); //1 inclusive, MAX_VAL exclusive
}
//Array add, sort, calculate time difference, add to total time
for (int i=0; i<10; i++) {
long beginArray= System.currentTimeMillis();
for (int m=0; m<50000000; m++) {
myArray[m]= randArray[m];
}
long endArray= System.currentTimeMillis();
Arrays.sort(myArray);
long endSortingArray= System.currentTimeMillis();
totalArray+= endArray-beginArray;
totalSortArray+= endSortingArray-endArray;
//ArrayList add, sort, calculate time difference, add to total time
long beginList=System.currentTimeMillis();
for (int k=0; k<50000000; k++) {;
myList.add(randArray[k]);
}
long endList= System.currentTimeMillis();
Collections.sort(myList);
long endSortingList= System.currentTimeMillis();
myList.clear();
totalList+= endList-beginList;
totalSortList+= endSortingList-endList;
//Vector add, sort, calculate time difference, add to total time
long beginVector= System.currentTimeMillis();
for (int l=0; l<50000000; l++) {
myVector.add(randArray[l]);
}
long endVector= System.currentTimeMillis();
Collections.sort(myVector);
long endSortingVector= System.currentTimeMillis();
myVector.clear();
totalVector+= endVector-beginVector;
totalSortVector+= endSortingVector-endVector;
}
//print add and sort times
System.out.println("Array add time: " + totalArray/10.0 + " ms");
System.out.println("ArrayList add time: " + totalList/10.0 + " ms");
System.out.println("Vector add time: " + totalVector/10.0 + " ms");
System.out.println("Array sort time: " + totalSortArray/10.0 + " ms");
System.out.println("ArrayList sort time: " + totalSortList/10.0 + " ms");
System.out.println("Vector sort time: " + totalSortVector/10.0 + " ms");
}
}