Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Boost Efficiency with a Performance-tuned Debugging Strategy-3 : Page 3


advertisement

Putting It to the Test
In the previous section, I made some bold claims about the inefficiency of some common coding practices and debugging techniques. Here is where I support those claims.

I wrote a test application called DebugTester to compare the two debugging techniques. Two identical loops execute a specified number of times. Each loop utilizes a different debugging technique. One loop creates and passes a String object to a logging method—the way that most debugging techniques are implemented, while the other checks a static final boolean variable first, to determine if debugging is turned on. Here is the source code for the class:

public class DebugTester
{
        public static final boolean DEBUG = false;



public static void main( String[] args ) { long begin, end; int max = 500000;

if ( args.length == 1 ) int max = Integer.parseInt( args[0] );

begin = System.currentTimeMillis(); for ( int i = 0; i < max; i++ ) logIt( "The current value of i is " + i ); end = System.currentTimeMillis(); System.out.println( "The first loop executed in " +
(end - begin) );

begin = System.currentTimeMillis(); for ( int i = 0; i < max; i++ ) { if ( DEBUG ) logIt( "The code won't get to here." ); } end = System.currentTimeMillis(); System.out.println( "The second loop executed in " +
(end - begin) );

} //end main()

public static void logIt( String msg ) { if ( DEBUG ) System.out.println( msg ); } //end logIt()

} //end DebugTester

The calls to "System.currentTimeMillis()" return the current system time in milliseconds. By checking this before and after each loop and comparing the difference between the values, the execution time of each loop can be determined.

The following table shows the results of executing the program with increasing numbers of loop iterations:

Clearly, these numbers are going to vary, depending upon the speed of your system's processor, what compiler you are using, and how your JVM happens to prioritize the processes involved. The last factor is especially important. The processes that the JVM goes through to determine processor priority will produce slightly different results on the same machine, testing the same input multiple times.

The important thing to notice is that it took 4 million iterations through the loop before the optimized technique even registered as taking any time at all! How can this be possible? The key is the final static boolean variable DEBUG. A good compiler will recognize this as a constant and will compile the code to read:

if ( false )
	logIt( "The code won't get to here" );

So when debugging is turned off, any lines of debugging that appear in your code effectively disappear!



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date