Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Design for performance, Part 2: Reduce object creation

Avoid performance hazards while designing Java classes

Though many programmers put off performance management until late in the development process, performance considerations should be integrated into the design cycle from day one. This series explores some of the ways in which early design decisions can significantly affect application performance. In this article, I continue to explore the problem of excessive temporary object creation and offer several proven techniques for avoiding their creation.

Read the whole "Design for Performance" series:



Temporary objects are those that have a short lifetime and generally serve no useful purpose other than to act as containers for other data. Programmers generally use temporary objects to pass compound data to -- or return it from -- a method. Part 1 explored how temporary object creations could have a serious negative impact on a program's performance and showed how certain class interface design decisions virtually guarantee temporary object creation. By avoiding those interface constructs, you can dramatically reduce the need to create temporary objects that can sap your program's performance.

Just say no to String?

When it comes to creating temporary objects, the String class is one of the biggest offenders. To illustrate that, in Part 1 I developed an example of a regular expression-matching class and showed how a harmless-looking interface imposed enough object-creation overhead to make it run several times slower than a similar class with a more carefully designed interface. Here are the interfaces for both the original and the better-performing classes:

BadRegExpMatcher

public class BadRegExpMatcher { 
  public BadRegExpMatcher(String regExp);
  /** Attempts to match the specified regular expression against the input
      text, returning the matched text if possible or null if not */
  public String match(String inputText);
}


BetterRegExpMatcher

class BetterRegExpMatcher { 
  public BetterRegExpMatcher(...);
  /** Provide matchers for multiple formats of input -- String,
      character array, and subset of character array.  Return -1 if no
      match was made; return offset of match start if a match was
      made.  */
  public int match(String inputText);
  public int match(char[] inputText);
  public int match(char[] inputText, int offset, int length);
  /** If a match was made, returns the length of the match; between
      the offset and the length, the caller should be able to
      reconstruct the match text from the offset and length */
  public int getMatchLength();
  /** Convenience routine to get the match string, in the event the
      caller happens to wants a String */
  public String getMatchText();
}


Programs that heavily use BadRegExpMatcher will run slower than those using BetterRegExpMatcher. First, callers have to create a String object to pass into match(), which then has to create another String object to return the matched text to the caller. That results in at least two object creations per invocation, which may not sound like much, but if you call match() frequently, the performance overhead of those object creations can really add up. The problem with BadRegExpMatcher's performance is not in its implementation but in its interface; with the interface defined as it is, there is no way to avoid creating several temporary objects.

1 | 2 | 3 | 4 | 5 |  Next >
Resources
  • Books on Java performance: