FeatureUSENIX

 

java performance

mccluskey_glen

by Glen McCluskey
<[email protected]>

Glen McCluskey is a consultant with 15 years of experience and has focused on programming languages since 1988. He specializes in Java and C++ performance, testing, and technical documentation areas.


In a previous column we looked at performance issues with Java I/O. Another aspect of I/O that needs to be mentioned is the cost of data formatting.

Consider first a couple of C examples, ones that output lines of the form:

 The maximum weight is 100 lbs.

The first example simply prints this string repetitively:

include <stdio.h>
int main()
{
 const long N = 100000L;
 long i;
 for (i = 1; i <= N; i++)
  printf("The maximum weight is 100 lbs.\n");
 return 0;
}

The second uses printf() to format the weight and units values:

#include <stdio.h>
int main()
{
 const long N = 100000L;
 long i;
 int w = 100;
 char* u = "lbs.";
 for (i = 1; i <= N; i++)
  printf("The maximum weight is %d %s\n", w, u);
 return 0;
}

The second program runs around 25 percent slower than the first because of the formatting overhead. Formatting is clearly a useful feature, but it's worth knowing what the costs are.

With Java, a similar issue arises. To determine how expensive formatting is, we can write a series of programs. The first prints a string over and over:

public class format1 {
 public static void main(String args[])
 {
  final int N = 25000;
  for (int i = 1; i <= N; i++) {
   String s = "The maximum weight is 100 lbs.\n";
   System.out.print(s);
  }
 }
}

The second uses the string concatenation operator (+) to format values of variables:

public class format2 {
 public static void main(String args[])
 {
  final int N = 25000;

  int w = 100;
  String u = "lbs.";
  for (int i = 1; i <= N; i++) {
   String s = "The maximum weight is " + w +
    " " + u + "\n";
   System.out.print(s);
  }
 }
}

This code is slower than the first example because of the extra costs of converting integer values like "w" to strings, and because of costs in concatenating strings.

The third example uses the Text.MessageFormat facility:

import java.text.*;
public class format3 {
 public static void main(String args[])
 {
  final int N = 25000;
  MessageFormat f = new
   MessageFormat("The maximum weight is {0} {1}\n");
  int w = 100;
  String u = "lbs.";
  Object vals[] = new Object[2];
  vals[0] = new Integer(w);
  vals[1] = u;
  for (int i = 1; i <= N; i++) {
   String s = f.format(vals);
   System.out.print(s);
  }
 }
}

The idea here is that a format object of type MessageFormat is created and then used. Placeholders like "{0}" in the format are replaced with object values passed to the
format() method. This approach is desirable if you're trying to write Java applications that work in international contexts; the message format can be read from a resource bundle (a group of resources that is keyed off of a locale) and then applied.

The final example is similar to the previous one, but it creates a message format on the fly:

import java.text.*;
public class format4 {
 public static void main(String args[])
 {
  final int N = 25000;
  String f = "The maximum weight is {0} {1}\n";
  int w = 100;
  String u = "lbs.";
  Object vals[] = new Object[2];
  vals[0] = new Integer(w);
  vals[1] = u;
  for (int i = 1; i <= N; i++) {
   String s = MessageFormat.format(f, vals);
   System.out.print(s);
  }
 }
}

In the previous example the message format was created once and then applied repetitively, allowing for some optimizations to be done. In this example, the format is passed in as a raw string each time. This approach is simpler but slower.

The programs produce identical output. The running times are:

 format1  1.3 (seconds)

 format2  1.9

 format3  5.5

 format4  8.0

If you're trying to tune I/O performance in a Java application, the area of data formatting may be worth looking at.

 

?Need help? Use our Contacts page.
First posted: 14 Apr. 1999 jr
Last changed: 14 Apr. 1999 jr
Issue index
;login: index
USENIX home