Locating the bottleneck | CodeGuru

Locating the bottleneck

Bruce Eckel’s Thinking in Java Contents | Prev | Next Three approaches to locating the performance-critical part of a program are: 1. Install your own instrumentation “Profile” code by inserting explicit timing: long start = System.currentTimeMillis(); // Operation to be timed goes here long time = System.currentTimeMillis() – start; Have an infrequently-used method print cumulative […]

Written By
CodeGuru Staff
CodeGuru Staff
Mar 1, 2001
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Three


approaches to locating the performance-critical part of a program are:


1.
Install your own instrumentation

“Profile”


code by inserting explicit timing:

long start = System.currentTimeMillis();
   // Operation to be timed goes here
long time = System.currentTimeMillis() - start;

Have


an infrequently-used method print cumulative times out to the console window


with


System.out.println( ).

Since the compiler will ignore it when false, a


static
final boolean

switch can turn the timing on and off so the code can efficiently be left in


place in released code, ready for emergency use at any time. Even when more


sophisticated profiling is available, this is a convenient way to time a


specific task or operation.

System.currentTimeMillis( )

returns time in 1/1000ths of a second. However, some systems with time


resolution less than a millisecond (such as a Windows PC) need to repeat an


operation


n

times and divide the total time by


n

to get accurate estimates.


2.
JDK profiling [2]

The


JDK comes with a built-in profiler that keeps track of the time spent in each


routine and writes the information to a file. Unfortunately, the JDK profilers


have uneven performance. JDK 1.1.1 works, but subsequent releases have had


various instabilities.

To


run the profiler, use the


-prof

option


when invoking the unoptimized versions of the Java interpreter, for example:

java_g
-prof myClass

Or


with an applet:

java_g
-prof sun.applet.AppletViewer applet.html

The


profiler output is not particularly easy to decipher. In fact, in JDK 1.0 it


truncates the method names to 30 characters, so it might not be possible to


distinguish between some methods. However, if your platform does support the


-prof

option, either Vladimir Bulatov’s


HyperProf

[3]


or Greg White’s


ProfileViewer

[4] will help interpret the results.


Advertisement

3.
Special tools

The


best way to keep up with the exploding field of performance optimization tools


is through a Web site such as Jonathan Hardwick’s


Tools
for Optimizing

Java

[5]


at


http://www.cs.cmu.edu/~jch/java/tools.html

.

Tips
for measuring performance

  • Since
    profiling uses clock time, make every effort to remove other processes during
    the measurement.
  • Always
    time the code before and after making changes to verify that, at least on the
    test platform, your changes improved the program. (Jon Bentley mentioned that
    some of his most logical changes actually slowed the program down.)
  • Try
    to make each timing test under identical conditions.
  • If
    possible, contrive a test that doesn’t rely on any user input to avoid
    variations in user response that can cause the results to fluctuate.
Contents

|

Prev

|

Next
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.