The Artima Developer Community
Sponsored Link

Java Community News
Brian Goetz on Writing Faster Java Code

5 replies on 1 page. Most recent reply: Mar 6, 2007 5:40 PM by James Watson

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Frank Sommers

Posts: 2642
Nickname: fsommers
Registered: Jan, 2002

Brian Goetz on Writing Faster Java Code Posted: Mar 5, 2007 11:29 AM
Reply to this message Reply
Summary
Sun's Java Developer Network recently interviewed Brian Goetz, author of Java Concurrency in Practice, and a Sun Java evangelist. The interview focused on writing faster-performing Java applications.
Advertisement

Sun's Java Developer Network interviewed Brian Goetz, author of Java Concurrency in Practice, and currently a Sun technology evangelist. The interview, Writing Better Code: A Conversation With Sun Microsystems Technology Evangelist Brian Goetz, focused on Java performance, one of Goetz's specialties.

In the interview, Goetz gave this advice on how to write faster-performing Java code:

Often, the way to write fast code in Java applications is to write dumb code—code that is straightforward, clean, and follows the most obvious object-oriented principles.

This has to do with the nature of dynamic compilers, which are big pattern-matching engines. Because compilers are written by humans who have schedules and time budgets, the compiler developers focus their efforts on the most common code patterns, because that's where they get the most leverage. So if you write code using straightforward object-oriented principles, you'll get better compiler optimization than if you write gnarly, hacked-up, bit-banging code that looks really clever but that the compiler can't optimize effectively.

So clean, dumb code often runs faster than really clever code, contrary to what developing in C might have taught us. In C, clever source code turns into the expected idiom at the machine-code level, but it doesn't work that way in Java applications...

If I could wave a magic wand and send out one message about Java programming, it would be this: Trust the JVM. It's smarter than you think. Stop trying to outwit or outsmart it. Tell it what you want, and it will do its damnedest to make your application run as fast as it can.

Does your experience confirm Goetz's admonition that straightforward Java code runs faster than code specially tweaked with performance in mind?


James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: Brian Goetz on Writing Faster Java Code Posted: Mar 5, 2007 11:59 AM
Reply to this message Reply
I think it's easy to read into what he's said. Some things are just not going to perform well like doing a binary sort on a linked list. The compiler is unlikely to optimize this. If they did that, I'd be impressed. So the code (Collections.binarySort()) has to make allowances for it.

But in terms of people doing things like avoiding using Objects because they think the overhead of Objects is great, I completely agree. I'm not sure whether it's the compiler but I've found that clean designs produces better performance partly because the resulting code tends to have better time-complexity at a high-level.

adrian milliner

Posts: 13
Nickname: goron
Registered: Feb, 2007

Re: Brian Goetz on Writing Faster Java Code Posted: Mar 5, 2007 2:10 PM
Reply to this message Reply
In general this is true. Don't prematurely optimise. Use a profiler to find cases where performance can be improved (if necessary).

This is hardly a revelation.

rody piyasin

Posts: 1
Nickname: rody
Registered: Mar, 2004

Re: Brian Goetz on Writing Faster Java Code Posted: Mar 5, 2007 2:19 PM
Reply to this message Reply
About optimization is really true, I have experienced it myself.

Erik Engbrecht

Posts: 210
Nickname: eengbrec
Registered: Apr, 2006

Re: Brian Goetz on Writing Faster Java Code Posted: Mar 5, 2007 7:02 PM
Reply to this message Reply
> I think it's easy to read into what he's said. Some
> things are just not going to perform well like doing a
> binary sort on a linked list. The compiler is unlikely to
> optimize this. If they did that, I'd be impressed. So
> the code (Collections.binarySort()) has to make allowances
> for it.
>
> But in terms of people doing things like avoiding using
> Objects because they think the overhead of Objects is
> great, I completely agree. I'm not sure whether it's the
> compiler but I've found that clean designs produces better
> performance partly because the resulting code tends to
> have better time-complexity at a high-level.


What's a binary sort? Last I checked there was a binary search but I've never heard of a binary sort....I thought the normal sort (for collections with constant-time random access) was merge sort... ;-)

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: Brian Goetz on Writing Faster Java Code Posted: Mar 6, 2007 5:40 PM
Reply to this message Reply
> What's a binary sort? Last I checked there was a binary
> search but I've never heard of a binary sort....I thought
> the normal sort (for collections with constant-time random
> access) was merge sort... ;-)

Actually I just mixed up the names. There are two methods in the JDKs Collections class: sort and binarySearch. Binary search is not a common term but I consider a tree-sort to be a 'binary sort'.

Flat View: This topic has 5 replies on 1 page
Topic: Desklets for Java Previous Topic   Next Topic Topic: NetBeans 6 Milestone Features Full Ruby Support

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use