threads

Synchronized/Unsynchronized collection performance test

This is an example of how to test the performance of synchronized and unsynchronized Collection. The test is described below:

  • We have a static method, performTest(List l) that adds a new Integer to the specified list for a specified int number of loops.
  • We have also created a class, Timestamp, that in its start() and stop() methods gets the System.nanoTime() and in its elapsedTime() method converts the subtraction between start and end time and uses convert(long sourceDuration, TimeUnit sourceUnit) method of TimeUnit to convert time duration to the given TimeUnit.
  • We also have created another method cleanGCollect(), that calls gc(), runFinilization() and again gc() API methods of System in order to run the garbage collector and run the finalization methods of any objects pending finalization.
  • We call the performTest(List l) method for a Vector, an ArrayList, a synchronized ArrayList, each time calling the Timestamp class to count the time each method takes to execute.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.TimeUnit;

public class CollectionExample {

    static int loops;

    public static void main(String[] args) {

  loops = 10;

  performTest(new Vector());

  performTest(new ArrayList());

  performTest(Collections.synchronizedList(new ArrayList()));

  System.out.println("Synchronized Vector test");

  cleanGCollect();

  Timestamp timSTM = new Timestamp();

  performTest(new Vector());

  timSTM.stop();

  System.out.println("Test took " + timSTM);

  System.out.println("Unsynchronized vector test");

  cleanGCollect();

  Timestamp timeSTMUnsync = new Timestamp();

  timeSTMUnsync.stop();

  System.out.println("Test took " + timeSTMUnsync);

  double gained = ((double) (timSTM.elapsedTime() - timeSTMUnsync.elapsedTime()))

    / loops;

  System.out.println("Unsynchronized operation saves " + gained + " "

    + timSTM.units() + " per call");

  System.out.println("Synchronized arraylist test");

  cleanGCollect();

  timSTM = new Timestamp();

  performTest(Collections.synchronizedList(new ArrayList()));

  timSTM.stop();

  System.out.println("Test took " + timSTM);

  System.out.println("Unsynchronized arraylist test");

  cleanGCollect();

  timeSTMUnsync = new Timestamp();

  performTest(new ArrayList());

  timeSTMUnsync.stop();

  System.out.println("Test took " + timeSTMUnsync);

  gained = ((double) (timSTM.elapsedTime() - timeSTMUnsync.elapsedTime())) / loops;

  System.out.println("Unsynchronized operation saves " + gained + " "

    + timSTM.units() + " per call");
    }

    static void cleanGCollect() {

  System.gc();

  System.runFinalization();

  System.gc();
    }

    static void performTest(List l) {

  Integer integer = new Integer(0);

  for (int i = 0; i < loops; i++) {

l.add(integer);

  }
    }
}
class Timestamp {

    private long start;
    private long stop;
    private boolean stopped = false;
    private TimeUnit timeUnit;

    public Timestamp() {

  this(TimeUnit.NANOSECONDS);
    }

    public Timestamp(TimeUnit units) {

  this.timeUnit = units;

  start();
    }

    public void start() {

  start = System.nanoTime();

  stopped = false;
    }

    public void stop() {

  stop = System.nanoTime();

  stopped = true;
    }

    public long elapsedTime() {

  if (!stopped) {

throw new IllegalStateException("Error while stoping timestamp");

  }

  return timeUnit.convert(stop - start, TimeUnit.NANOSECONDS);
    }

    @Override
    public String toString() {

  try {

return elapsedTime() + " " + timeUnit;

  } catch (IllegalStateException ex) {

return " Timestamp didn't stop";

  }
    }

    public String units() {

  return timeUnit.toString();
    }
}

Output:

Synchronized Vector test Test took 6187 NANOSECONDS Unsynchronized vector test Test took 309 NANOSECONDS Unsynchronized operation saves 587.8 NANOSECONDS per call Synchronized arraylist test Test took 8354 NANOSECONDS Unsynchronized arraylist test Test took 5878 NANOSECONDS Unsynchronized operation saves 247.6 NANOSECONDS per call

 
This was an example of how to test the performance of synchronized and unsynchronized Collection in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button