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

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

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

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}