Skip to content

Java Collections – Performances – Part II

September 15, 2012

In the earlier version of this exercise, did it for few collections alone.

I haven’t tried anything on the linkedlist and other collections.

Now, have included whatever possible.

 

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;

import org.perf4j.StopWatch;
import org.perf4j.log4j.Log4JStopWatch;

public class JavaCollections {
private static int count = 20;

public static void main(String[] args) {
stackMan();
listMan();
linkedListMan();
linkedHashMapMan();
treeSetMan();
linkedBlockingDequeMan();
arrayBlockingQueueMan();
concurrentLinkedQueueMan();
delayQueueMan();
treeMapMan();
linkedBlockingQueueMan();
priorityBlockingQueueMan();
priorityQueueMan();
concurrentSkipListSetMan();
copyOnWriteArraySetMan();
hashSetMan();
arrayDequeMan();
}

private static void stackMan() {
StopWatch stopWatch = new Log4JStopWatch();
Stack<String> stackContents = new Stack<String>();
for (int i = 0; i < count; i++) {
stackContents.add(i, Integer.toString(i));
}
stopWatch.stop(“stackMan”);
}

private static void listMan() {
StopWatch stopWatch = new Log4JStopWatch();
ArrayList<String> listContents = new ArrayList<String>();
for (int i = 0; i < count; i++) {
listContents.add(i, Integer.toString(i));
}
stopWatch.stop(“ListMan”);
}

private static void linkedListMan() {
StopWatch stopWatch = new Log4JStopWatch();
LinkedList<String> linkedListContents = new LinkedList<String>();
for (int i = 0; i < count; i++) {
linkedListContents.add(i, Integer.toString(i));
}
stopWatch.stop(“LinkedListMan”);
}

private static void linkedHashMapMan() {
StopWatch stopWatch = new Log4JStopWatch();
LinkedHashMap<Integer, String> linkedhashMap = new LinkedHashMap<Integer, String>();
for (int i = 0; i < count; i++) {
linkedhashMap.put(Integer.valueOf(i), Integer.toString(i));
}
stopWatch.stop(“linkedHashMapMan”);
}

private static void treeSetMan() {
StopWatch stopWatch = new Log4JStopWatch();
TreeSet<String> treeSet = new TreeSet<String>();
for (int i = 0; i < count; i++) {
treeSet.add(Integer.toString(i));
}
stopWatch.stop(“treeSetMan”);
}

private static void arrayBlockingQueueMan() {
StopWatch stopWatch = new Log4JStopWatch();
ArrayBlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<String>(
count);
for (int i = 0; i < count; i++) {
arrayBlockingQueue.add(Integer.toString(i));
}
stopWatch.stop(“arrayBlockingQueueMan”);
}

private static void concurrentLinkedQueueMan() {
StopWatch stopWatch = new Log4JStopWatch();
ConcurrentLinkedQueue<String> concurrentLinkedQueue = new ConcurrentLinkedQueue<String>();
for (int i = 0; i < count; i++) {
concurrentLinkedQueue.add(Integer.toString(i));
}
stopWatch.stop(“concurrentLinkedQueueMan”);
}

private static void delayQueueMan() {
StopWatch stopWatch = new Log4JStopWatch();
DelayQueue<JaganString> delayQueue = new DelayQueue<JaganString>();
JaganString jString = new JaganString();
for (int i = 0; i < count; i++) {
jString.setText(Integer.toString(i));
delayQueue.add(jString);
}
stopWatch.stop(“delayQueueMan”);
}

private static void linkedBlockingDequeMan() {
StopWatch stopWatch = new Log4JStopWatch();
LinkedBlockingDeque<String> linkedBlockingDeque = new LinkedBlockingDeque<String>();
for (int i = 0; i < count; i++) {
linkedBlockingDeque.add(Integer.toString(i));
}
stopWatch.stop(“linkedBlockingDeque”);
}

private static void treeMapMan() {
StopWatch stopWatch = new Log4JStopWatch();
TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();
for (int i = 0; i < count; i++) {
treeMap.put(Integer.valueOf(i), Integer.toString(i));
}
stopWatch.stop(“treeMapMan”);
}

private static void linkedBlockingQueueMan() {
StopWatch stopWatch = new Log4JStopWatch();
LinkedBlockingQueue<String> linkedBlockingQueue = new LinkedBlockingQueue<String>();
for (int i = 0; i < count; i++) {
linkedBlockingQueue.add(Integer.toString(i));
}
stopWatch.stop(“linkedBlockingQueueMan”);
}

private static void priorityBlockingQueueMan() {
StopWatch stopWatch = new Log4JStopWatch();
PriorityBlockingQueue<String> priorityBlockingQueue = new PriorityBlockingQueue<String>();
for (int i = 0; i < count; i++) {
priorityBlockingQueue.add(Integer.toString(i));
}
stopWatch.stop(“priorityBlockingQueueMan”);
}

private static void priorityQueueMan() {
StopWatch stopWatch = new Log4JStopWatch();
PriorityQueue<String> priorityQueue = new PriorityQueue<String>();
for (int i = 0; i < count; i++) {
priorityQueue.add(Integer.toString(i));
}
stopWatch.stop(“priorityQueueMan”);
}

private static void concurrentSkipListSetMan() {
StopWatch stopWatch = new Log4JStopWatch();
ConcurrentSkipListSet<String> concurrentSkipListSet = new ConcurrentSkipListSet<String>();
for (int i = 0; i < count; i++) {
concurrentSkipListSet.add(Integer.toString(i));
}
stopWatch.stop(“concurrentSkipListSetMan”);
}

private static void copyOnWriteArraySetMan() {
StopWatch stopWatch = new Log4JStopWatch();
CopyOnWriteArraySet<String> copyOnWriteArraySet = new CopyOnWriteArraySet<String>();
for (int i = 0; i < count; i++) {
copyOnWriteArraySet.add(Integer.toString(i));
}
stopWatch.stop(“copyOnWriteArraySetMan”);
}

private static void hashSetMan() {
StopWatch stopWatch = new Log4JStopWatch();
HashSet<String> hashSet = new HashSet<String>();
for (int i = 0; i < count; i++) {
hashSet.add(Integer.toString(i));
}
stopWatch.stop(“hashSetMan”);
}

private static void arrayDequeMan() {
StopWatch stopWatch = new Log4JStopWatch();
ArrayDeque<String> arrayDeque = new ArrayDeque<String>();
for (int i = 0; i < count; i++) {
arrayDeque.add(Integer.toString(i));
}
stopWatch.stop(“arrayDequeMan”);
}

}

We need to have an object which is implementing Delayed. Got the reference from http://sungur.wordpress.com/2011/02/10/java-delayqueue-test/ and implemented the following

 

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class JaganString implements Delayed {

private long endOfDelay;
private String text;
private long queueInsertTime;

public long getQueueInsertTime() {
return queueInsertTime;
}

public void setQueueInsertTime(long queueInsertTime) {
this.queueInsertTime = queueInsertTime;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public JaganString() {
// TODO Auto-generated constructor stub
}

public long getEndOfDelay() {
return endOfDelay;
}

public void setEndOfDelay(long endOfDelay) {
this.endOfDelay = endOfDelay;
}

@Override
public long getDelay(TimeUnit unit) {
// TODO Auto-generated method stub
long tmp = unit.convert(
(getQueueInsertTime() – System.currentTimeMillis())
+ endOfDelay, TimeUnit.MILLISECONDS);
return tmp;

}

@Override
public int compareTo(Delayed o) {
// TODO Auto-generated method stub
int ret = 0;
JaganString ns = (JaganString) o;

if (this.endOfDelay < ns.endOfDelay)
ret = -1;
else if (this.endOfDelay > ns.endOfDelay)
ret = 1;
else if (this.getQueueInsertTime() == ns.getQueueInsertTime())
ret = 0;

return ret;

}

}

Ran the program with Sizes – 2000, 20000, 20000 and the results are as follows

Tag Avg(ms) – 2000 Avg(ms) – 20000   Avg(ms) – 200000
arrayBlockingQueueMan 1 4 56
arrayDequeMan 0 4 33
concurrentLinkedQueueMan 2 4 36
concurrentSkipListSetMan 7 25 398
copyOnWriteArraySetMan 45 4003 460606
delayQueueMan 4 15 67
hashSetMan 0 4 79
linkedBlockingDeque 4 9 242
linkedBlockingQueueMan 3 5 87
linkedHashMapMan 3 8 404
LinkedListMan 1 7 145
ListMan 1 5 2503
priorityBlockingQueueMan 2 11 142
priorityQueueMan 0 5 26
stackMan 2 7 63
treeMapMan 1 10 234
treeSetMan 5 21 318

 

Some graphical representations

 

 

 

What’s next?

1. Compare the Java implementations of Concurrent Classes with Scala Library?

2. Do we get any performance benefit in having multi-core processors?

3. Concurrent collections with Multi-threaded applications

 

 

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: