DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Synchronized is Slower but not Worth Worrying About

Synchronized is Slower but not Worth Worrying About

Peter Lawrey user avatar by
Peter Lawrey
·
Dec. 19, 11 · Java Zone · Interview
Like (0)
Save
Tweet
3.88K Views

Join the DZone community and get the full member experience.

Join For Free

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

Data structure application Threading Comparison (grammar) Clear (Unix) Measure (physics) Task (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • PermGen and Metaspace
  • Version Number Anti-Patterns
  • Introduction to Word Vectors
  • Correlation Between Fast TTM and Containers

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo