HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Benchmarking with an Abstract Class"
Volume: 4 Issue: 7, p. 26

	

Listing 1: 

class HowFast { 
   public static void main (String[] args) { 
      long timeBefore, timeAfter; 

      timeBefore = System.currentTimeMillis(); 

      // InFile class explained below 
      int result 
         = InFile.numBytes ("c:\\temp\\te_employee.tps"); 

      timeAfter = System.currentTimeMillis(); 

      System.out.println (timeAfter - timeBefore); 
   } 
} 

Listing 2: 

import java.io.*; 
import java.util.*; 

class InFile { 
/* 
/  Author: Steven Feuerstein 
/    Date: 12/25/98 
*/ 
   public static int numBytes (String filename) 
      //throws IOException 
   { 
   try 
      { 
      InputStream in; 

      if (filename.length() == 0) 
         in = System.in; 
      else 
         in = new FileInputStream (filename); 

      int total = 0; 

      while (in.read () != -1) 
         total++; 

      return total; 
      } 
      catch (IOException e) 
      { 
         return -1; 
      } 
 } 

   public static int numBytes2 (String filename) 
      //throws IOException 
   { 
   try 
      { 
      FileInputStream in = new FileInputStream (filename); 

      // Available is good for use with files, since the whole file 
      // is generally available. 
      return in.available(); 
      } 
      catch (IOException e) 
      { 
         return -1; 
      } 
 } 

   public static void main (String args[]) { 
      System.out.println ("Size of " + args[0] + ": " + numBytes2(args[0])); 
      System.out.println ("Size of " + args[0] + ": " + numBytes(args[0])); 
   } 
} 

Listing 3: 

class Timer { 
/* 
*  A package to calculate the elapsed time of code execution. 
* 
*  Author: Steven Feuerstein 
*    Date: 12/28/98 
*/ 
   // Start time elements 
   private long mstart = 0; 
   private boolean mstart_set = false; 

   // Stop time elements 
   private long mstop = 0; 
   private boolean mstop_set = false; 

   // Context, as in: description of the timing run 
   private String mcontext; 

   // Provide a constructor that automatically starts a 
   // timer session. 
   public Timer (String context) { 
      this.start (context); 
   } 

   public Timer () { this.start (""); } 

   // Start the "clock ticking". Of course, the clock is always 
   // ticking. This just captures the starting point. 
   public void start (String context) { 
      mstart = System.currentTimeMillis(); 
      mstart_set = true; 
      mstop_set = false; 

      if (context.length() > 0) 
         mcontext = "Elapsed from " + context; 
      else 
         mcontext = "Elapsed from start"; 
   } 

   // stop the timing run, setting the context if it is present. 
   public void stop (String context) { 
      if (mstart_set) 
      { 
         mstop = System.currentTimeMillis(); 
         mstop_set = true; 

         if (context.length() > 0) 
            mcontext = mcontext + " to " + context; 
         else 
            mcontext = mcontext + " to stop"; 
      } 
      else 
         System.out.println ("You must start the Timer before you can stop 
it."); 
   } 

   // Return the elapsed time, calling stop() if it hadn't been called 
   // since start() was executed. 
   public long elapsed (String context) { 
      if (mstop_set == false) stop (context); 
      return (mstop - mstart); 
   } 

   public String elapsedMessage (String context) { 
      // Shut down the timing ASAP to make results more accurate. 
      long elapsedVal = elapsed(context); 
      return (mcontext + ": " + elapsedVal + " millisecs"); 
   } 

   // Provide an implementation of toString that offers something 
   // relevant to the Timer. 
   public String toString () { return elapsedMessage(""); } 

   // Show the elapsed time, including the context information. 
   public void showElapsed (String context) { 
      System.out.println (elapsedMessage (context)); 
   } 

   // Overloadings when no context is provided. 
   public void start () { start (""); } 
   public void stop   () { stop ("");    } 
   public long elapsed () { return elapsed (""); } 
   public String elapsedMessage () { return elapsedMessage (""); } 
   public void showElapsed () { showElapsed(""); } 
} 

Listing 4: 

class HowFast2 { 
   public static void main (String[] args) { 
      int result; 
      Timer bruteForce = new Timer(); 
      Timer useAvailable = new Timer(); 

      bruteForce.start(); 

      result = InFile.numBytes ("c:\\temp\\te_employee.tps"); 

      bruteForce.showElapsed(); 

      useAvailable.start(); 

      result = InFile.numBytes2 ("c:\\temp\\te_employee.tps"); 

      useAvailable.showElapsed(); 
   } 
} 

Listing 5: 

class HowFast3 { 
   public static void main (String[] args) { 
      int result; 
      Timer bruteForce = new Timer(); 
      Timer useAvailable = new Timer(); 

      bruteForce.start(); 

      result = InFile.numBytes ("c:\\temp\\te_employee.tps"); 

      bruteForce.showElapsed("Countem"); 

      useAvailable.start(); 

      result = InFile.numBytes2 ("c:\\temp\\te_employee.tps"); 

      useAvailable.showElapsed("Available"); 
   } 
} 

Listing 6: 

class HowFast5 { 
   public static void main (String[] args) { 
      int count = Integer.parseInt (args[0]); 

      Timer bruteForce = new Timer(); 

      for (int execnum = 1; execnum <= count; execnum++) { 
         int result = InFile.numBytes ("c:\\temp\\te_event.pks"); 
      } 

      bruteForce.showElapsed("Countem"); 

      Timer useAvailable = new Timer(); 

      for (int execnum = 1; execnum <= count; execnum++) { 
         int result = InFile.numBytes2 ("c:\\temp\\te_event.pks"); 
      } 

      useAvailable.showElapsed("Available"); 
   } 
} 

Listing 7: 

class Christian extends Beliefs 
{ 
   String holyBook () { return "Bible"; }; 
   String dayOfPrayer () { return "Sunday"; }; 
   public String toString () { return "Christians"; }; 
} 

class Atheist extends Beliefs 
{ 
   String holyBook () { return "Non-Existent"; }; 
   String dayOfPrayer () { return "Not Applicable"; }; 
   public String toString () { return "Atheists"; }; 
} 

class Jew extends Beliefs 
{ 
   String holyBook () { return "Torah"; }; 
   String dayOfPrayer () { return "Saturday"; }; 
   public String toString () { return "Jews"; }; 
} 

class Muslim extends Beliefs 
{ 
   String holyBook () { return "Quran"; }; 
   String dayOfPrayer () { return "Friday"; }; 
   public String toString () { return "Muslims"; }; 
} 
  

Listing 8: 

abstract class RunTimer1x2 extends Timer { 
/* 
/  Performs repetitive testing of a single argument 
/  version of the timeIt method. 
/ 
/  Author: Steven Feuerstein 
/    Date: 12/28/98 
*/ 
   abstract void timeIt1 (Object arg1); 
   abstract void timeIt2 (Object arg1); 

   public void repeat1 (String context, int count, Object arg1) 
   { 
      super.start(); 
      for (int execnum = 0; execnum < count; execnum++) 
         timeIt1 (arg1); 
      showinfo (context, count); 
   } 

   public void repeat2 (String context, int count, Object arg1) 
   { 
      super.start(); 
      for (int execnum = 0; execnum < count; execnum++) 
         timeIt2 (arg1); 
      showinfo (context, count); 
   } 

   // Common code called by both repeat methods. 
   private void showinfo (String context, int count) 
   { 
      super.showElapsed(context); 
  

     System.out.println ( 
         "   Number of iterations: " + count); 

      System.out.println ( 
         "   Per iteration elapsed: " + (float)super.elapsed() / 
(float)count); 
   } 
} 

Listing 9: 

class TimeBoth extends RunTimer1x2 { 
   void timeIt1 (Object arg1) { 
      int result = InFile.numBytes (arg1.toString()); 
   } 

   void timeIt2 (Object arg1) { 
      int result = InFile.numBytes2 (arg1.toString()); 
   } 

   public static void main (String[] args) { 
      TimeBoth testIt = new TimeBoth(); 
      testIt.repeat1 ( 
         "Countem", Integer.parseInt (args[0]), args[1]); 
      testIt.repeat2 ( 
         "Available", Integer.parseInt (args[0]),  args[1]); 
   } 
} 
  

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: info@sys-con.com

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.