DEVELOPER: Optimization

Building Faster Java
By Harshad Oak

Code Listing 1: OptimizationTrial.java before first CodeCoach run

    package oramag.client;
    import java.io.IOException;

    import java.text.SimpleDateFormat;

    import java.util.ArrayList;
    import java.util.Date;

    public class OptimizationTrial {
        public OptimizationTrial() {
        }

        static int count=99999;

        public static void main(String[] args) throws IOException{
            todaysDay();
            demoArrayListOptimized(count);
            demoArrayList(count);
        }

        public static void todaysDay() throws IOException{
            Object today = fetchTodaysDay();
            if (today instanceof String) {
                System.out.println("The object 'today' is always a String. Check unnecessary");
            }

            System.out.println("Today is >>>" + today);
        }

        public static Object fetchTodaysDay() {
            SimpleDateFormat sfInput = new SimpleDateFormat("EEEEEEEEE");
            return sfInput.format(new Date());
        }

        public static void demoArrayList(int number) {
          System.out.println("Unoptimized Run");
          ArrayList ar= new ArrayList ();
          //NOTE: Change number to zero to demonstrate codecoach and audit features
          for(int i=0; i<0; i++) {
            ar.add(new Integer(i));
          }
        }

        public static void demoArrayListOptimized(int numberOpt) {
          System.out.println("Optimized Run");
          ArrayList ar= new ArrayList (numberOpt);
          for(int i=0; i<numberOpt; i++) {
            ar.add(new Integer(i));
          }
        }
    }

Code Listing 2: OptimizationTrial.java after CodeCoach optimization

    package oramag.client;
    import java.io.IOException;

    import java.text.SimpleDateFormat;

    import java.util.ArrayList;
    import java.util.Date;

    public final class OptimizationTrial {
        public OptimizationTrial() {
        }

        private static final int count=99999;

        public static void main(String[] args) throws IOException{
            todaysDay();
            demoArrayListOptimized(count);
            demoArrayList(count);
        }

        private static void todaysDay() throws IOException{
            Object today = fetchTodaysDay();
            //if (today instanceof String) {
                System.out.println("The object 'today' is always a String. Check unnecessary");
            //}

            System.out.println("Today is >>>" + today);
        }

        private static Object fetchTodaysDay() {
            SimpleDateFormat sfInput = new SimpleDateFormat("EEEEEEEEE");
            return sfInput.format(new Date());
        }

        private static void demoArrayList(int number) {
          System.out.println("Unoptimized Run");
          ArrayList ar= new ArrayList ();
          //NOTE: Change number to zero to demonstrate codecoach and audit features
          for(int i=0; i<number; i++) {
            ar.add(new Integer(i));
          }
        }

        private static void demoArrayListOptimized(int numberOpt) {
          System.out.println("Optimized Run");
          ArrayList ar= new ArrayList (numberOpt);
          for(int i=0; i<numberOpt; i++) {
            ar.add(new Integer(i));
          }
        }
    }