Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Synchronized is Slower but not Worth Worrying About

DZone's Guide to

Synchronized is Slower but not Worth Worrying About

· Java Zone
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

Often developers jump through hoops to avoid synchronization. There are justifications like; the application does lots of writes or reads. Without a clear idea of what "lots" means, they have no idea whether the optimisations they are using will help or just add complexity (and possibly make it slower)

 

Vector is slower than ArrayList, but that's not a good reason to avoid it

I prefer to use lock free objects and collections in a single threaded context. This is more for clarity than performance. It makes it obvious this code was designed to be single threaded. The performance difference by comparison is often trivial.

 

public static void main(String... args) throws IOException {
    for (int i = 0; i < 5; i++) {
        perfTest(new ArrayList());
        perfTest(new Vector());
    }
}

private static void perfTest(List objects) {
    long start = System.nanoTime();
    final int runs = 100000000;
    for (int i = 0; i < runs; i += 20) {
        // add items.
        for (int j = 0; j < 20; j+=2)
            objects.add(i);
        // remove from the end.
        while (!objects.isEmpty())
            objects.remove(objects.size() - 1);
    }
    long time = System.nanoTime() - start;
    System.out.printf("%s each add/remove took an average of %.1f ns%n", objects.getClass().getSimpleName(),  (double) time/runs);
}
prints
ArrayList each add/remove took an average of 3.0 ns
Vector each add/remove took an average of 38.5 ns
ArrayList each add/remove took an average of 2.0 ns
Vector each add/remove took an average of 4.4 ns
ArrayList each add/remove took an average of 2.4 ns
Vector each add/remove took an average of 4.6 ns
ArrayList each add/remove took an average of 2.0 ns
Vector each add/remove took an average of 4.6 ns
ArrayList each add/remove took an average of 2.3 ns
Vector each add/remove took an average of 4.5 ns
Using Vector is several times slower, and if all you application does is access the Vector it would make sense to use an ArrayList (and be glad you only have a trivial program to write) For real-world applications, a number of operations are needed and the cost of any one of these is relatively small.

The difference in time is only 2 ns (possibly more on some systems). If you have a task which takes a micro-second (called one million times per second) that operation will take 0.5% slower. If you have an operation which is used only 100,000 times per second, you might find you can't even measure the difference.

In summary, don't worry about performance problems until you have measured the difference you believe it would make. Otherwise you can waste a lot of time "optimising" something which won't make any difference or worse, is slower but you don't know it. (Due to the added complexity in "optimising" the solution)

 

From http://vanillajava.blogspot.com/2011/12/synchronized-is-slower-but-not-worth.html

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}