-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListTiming.java
More file actions
33 lines (30 loc) · 1023 Bytes
/
Copy pathLinkedListTiming.java
File metadata and controls
33 lines (30 loc) · 1023 Bytes
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
import java.util.Random;
import java.util.List;
import java.util.LinkedList;
import java.util.Collections;
public class LinkedListTiming{
public static void main(String[] args){
final int N = Integer.parseInt(args[0]);
final int ONE = 1;
List<Integer> list = new LinkedList<Integer>(Collections.nCopies(N, 0));
long timeStart = System.currentTimeMillis();
for(int i = 0; i < N; ++i ){
if(i % 1000 == 0){
System.out.println("processing iteration: " + i);
}
Random rn = new Random();
//The below should work in Java, but it doesn't. This is unexpected behavior - the mod operator should always return positive.
//int index = rn.nextInt() % N;
// To fix the problem do this:
int index = rn.nextInt() % N;
if(index < 0){
index += N;
}
// More about the issue:
//https://stackoverflow.com/questions/5385024/mod-in-java-produces-negative-numbers
list.add(index, ONE);
}
long timeEnd = System.currentTimeMillis();
System.out.println(timeEnd - timeStart);
}
}