Groups | Search | Server Info | Keyboard shortcuts | Login | Register


Groups > comp.lang.java.programmer > #5610

Passing a Method Name to a Method, Redux

From Gene Wirchenko <genew@ocis.net>
Newsgroups comp.lang.java.programmer
Subject Passing a Method Name to a Method, Redux
Date 2011-06-23 16:03 -0700
Organization A noiseless patient Spider
Message-ID <fpg7079ca2dtgipdphr8rm234kgmkd1t3l@4ax.com> (permalink)

Show all headers | View raw


Dear Java'ers:

     I have completed my benchmarking.  The code is below.  Note that
the difference in the bodies between ParseSequentialSearch(),
ParseBinarySearch(), and ParseTreesetSearch() is but one line.  I
really would have preferred not having to duplicate the code.

     Oddly, the timings have a LOT of noise in them.  In some runs, a
sequential search has out-performed a binary search.  Occasionally, a
sequential search has beaten both a binary search and a Treeset
search.  The times for sequential searching are only a bit worse than
for binary searching.  Treeset searching is about 20% faster.  Any
explanations?

     I had to kludge this:
          cIdent=""+CurrChar;
cIdent is a String, CurrChar is a char.
          cIdent=CurrChar;
does not compile.

     So how would you have written this benchmark?

***** Start of Code *****
// TimingTesting
// Timing Testing of Character Searching
// Last Modification: 2011-06-23



import java.util.*;



class TimingTesting
   {

   static String cParseString=
    "//identifier//IDENTIFIER//a_b_c abc123
4b5%$__dbl;one;two;three;END";

   static String IdentChars=
    "0123456789"+
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"+
    "_"+
    "abcdefghijklmnopqrstuvwxyz";     // sorted order!

   static SortedSet<Character> IdentCharsSet=new TreeSet<Character>();

   static int nRepetitions=1000000;



   static boolean SequentialSearch
    (
    char CurrChar
    )
      {
      boolean fFound=false;
      for (int i=0; i<IdentChars.length() && !fFound; i++)
         fFound=IdentChars.charAt(i)==CurrChar;
      return fFound;
      }



   static boolean BinarySearch
    (
    char CurrChar
    )
      {
      int xLow=0;
      int xHigh=IdentChars.length()-1;
      int xTry;
      boolean fFound=false;
      while (xLow<=xHigh)
         {
         xTry=(xLow+xHigh)/2;
         if (CurrChar==IdentChars.charAt(xTry))
            return true;
         if (CurrChar<IdentChars.charAt(xTry))
            xHigh=xTry-1;
         else
            xLow=xTry+1;
         }
      return false;
      }



   static boolean TreesetSearch
    (
    char CurrChar
    )
      {
      return IdentCharsSet.contains(CurrChar);
      }



   static void ParseSequentialSearch()
      {
      int xScan=0;
      boolean fBuildingIdent=false;
      boolean fInIdentChars;
      String cIdent="";     // fussy init
      while (xScan<cParseString.length())
         {
         char CurrChar=cParseString.charAt(xScan);
         fInIdentChars=SequentialSearch(CurrChar);
         if (SequentialSearch(CurrChar))     // different code
            if (fBuildingIdent)
               cIdent+=CurrChar;
            else
               {
               fBuildingIdent=true;
               cIdent=""+CurrChar;
               }
         else
            if (fBuildingIdent)
               {
               fBuildingIdent=false;
if (nRepetitions==1)
 System.out.println(cIdent);
               }
            else
               {}
         xScan++;
         }
      if (fBuildingIdent)
if (nRepetitions==1)
 System.out.println(cIdent);
      }



   static void ParseBinarySearch()
      {
      int xScan=0;
      boolean fBuildingIdent=false;
      boolean fInIdentChars;
      String cIdent="";     // fussy init
      while (xScan<cParseString.length())
         {
         char CurrChar=cParseString.charAt(xScan);
         fInIdentChars=SequentialSearch(CurrChar);
         if (SequentialSearch(CurrChar))     // different code
            if (fBuildingIdent)
               cIdent+=CurrChar;
            else
               {
               fBuildingIdent=true;
               cIdent=""+CurrChar;
               }
         else
            if (fBuildingIdent)
               {
               fBuildingIdent=false;
if (nRepetitions==1)
 System.out.println(cIdent);
               }
            else
               {}
         xScan++;
         }
      if (fBuildingIdent)
if (nRepetitions==1)
 System.out.println(cIdent);
      }



   static void ParseTreesetSearch()
      {
      int xScan=0;
      boolean fBuildingIdent=false;
      boolean fInIdentChars;
      String cIdent="";     // fussy init
      while (xScan<cParseString.length())
         {
         char CurrChar=cParseString.charAt(xScan);
         fInIdentChars=SequentialSearch(CurrChar);
         if (TreesetSearch(CurrChar))     // different code
            if (fBuildingIdent)
               cIdent+=CurrChar;
            else
               {
               fBuildingIdent=true;
               cIdent=""+CurrChar;
               }
         else
            if (fBuildingIdent)
               {
               fBuildingIdent=false;
if (nRepetitions==1)
 System.out.println(cIdent);
               }
            else
               {}
         xScan++;
         }
      if (fBuildingIdent)
if (nRepetitions==1)
 System.out.println(cIdent);
      }



   public static void main(String[] args)
      {
      int i;

      long StartTime;
      long EndTime;
      long Duration;

      System.out.println("Timing Testing of Character Searching");
      System.out.println();

      // Initialise Set.
      for (i=0; i<IdentChars.length(); i++)
         IdentCharsSet.add(IdentChars.charAt(i));

      // Character Sequential
      System.out.print("Character Sequential Search");
      StartTime=System.nanoTime();
      for (i=1; i<=nRepetitions; i++)
         ParseSequentialSearch();
      EndTime=System.nanoTime();
      Duration=EndTime-StartTime;
      System.out.println(" Duration="+Duration);

      // Character Binary Search
      System.out.print("Character Binary Search    ");
      StartTime=System.nanoTime();
      for (i=1; i<=nRepetitions; i++)
         ParseBinarySearch();
      EndTime=System.nanoTime();
      Duration=EndTime-StartTime;
      System.out.println(" Duration="+Duration);

      // Character Treeset
      System.out.print("Character Treeset Search   ");
      StartTime=System.nanoTime();
      for (i=1; i<=nRepetitions; i++)
         ParseTreesetSearch();
      EndTime=System.nanoTime();
      Duration=EndTime-StartTime;
      System.out.println(" Duration="+Duration);
      }

   }
***** End of Code *****

Sincerely,

Gene Wirchenko

Back to comp.lang.java.programmer | Previous | NextNext in thread | Find similar


Thread

Passing a Method Name to a Method, Redux Gene Wirchenko <genew@ocis.net> - 2011-06-23 16:03 -0700
  Re: Passing a Method Name to a Method, Redux Gene Wirchenko <genew@ocis.net> - 2011-06-23 16:26 -0700
    Re: Passing a Method Name to a Method, Redux blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-06-27 21:41 +0000
  Re: Passing a Method Name to a Method, Redux markspace <-@.> - 2011-06-23 17:24 -0700
    Re: Passing a Method Name to a Method, Redux Gene Wirchenko <genew@ocis.net> - 2011-06-23 19:46 -0700
      Re: Passing a Method Name to a Method, Redux blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-07-04 03:26 +0000
        Re: Passing a Method Name to a Method, Redux lewbloch <lewbloch@gmail.com> - 2011-07-04 03:41 -0700
          Re: Passing a Method Name to a Method, Redux blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-07-05 19:07 +0000
  Re: Passing a Method Name to a Method, Redux markspace <-@.> - 2011-06-23 17:34 -0700
    Re: Passing a Method Name to a Method, Redux Gene Wirchenko <genew@ocis.net> - 2011-06-23 19:42 -0700
  Re: Passing a Method Name to a Method, Redux markspace <-@.> - 2011-06-23 18:30 -0700
    Re: Passing a Method Name to a Method, Redux Gene Wirchenko <genew@ocis.net> - 2011-06-23 19:48 -0700
      Re: Passing a Method Name to a Method, Redux markspace <-@.> - 2011-06-23 21:02 -0700
        Re: Passing a Method Name to a Method, Redux lewbloch <lewbloch@gmail.com> - 2011-06-24 08:38 -0700
          Re: Passing a Method Name to a Method, Redux markspace <-@.> - 2011-06-24 09:04 -0700
            Re: Passing a Method Name to a Method, Redux Lew <noone@lewscanon.com> - 2011-06-26 13:43 -0400
              Re: Passing a Method Name to a Method, Redux Lew <noone@lewscanon.com> - 2011-06-26 14:31 -0400
        Re: Passing a Method Name to a Method, Redux Gene Wirchenko <genew@ocis.net> - 2011-06-24 11:45 -0700
          Re: Passing a Method Name to a Method, Redux markspace <-@.> - 2011-06-24 12:19 -0700
            Re: Passing a Method Name to a Method, Redux Gene Wirchenko <genew@ocis.net> - 2011-06-26 20:39 -0700
              Re: Passing a Method Name to a Method, Redux markspace <-@.> - 2011-06-26 23:33 -0700
                Re: Passing a Method Name to a Method, Redux Gene Wirchenko <genew@ocis.net> - 2011-06-27 13:53 -0700
                Re: Passing a Method Name to a Method, Redux markspace <-@.> - 2011-06-27 18:03 -0700
                Re: Passing a Method Name to a Method, Redux Gene Wirchenko <genew@ocis.net> - 2011-06-28 11:41 -0700
        Re: Passing a Method Name to a Method, Redux blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-06-24 19:19 +0000
  Re: Passing a Method Name to a Method, Redux Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-06-23 19:36 -0700
    Re: Passing a Method Name to a Method, Redux Gene Wirchenko <genew@ocis.net> - 2011-06-24 11:50 -0700
      Re: Passing a Method Name to a Method, Redux Jeff Higgins <jeff@invalid.invalid> - 2011-06-24 17:25 -0400
        Re: Passing a Method Name to a Method, Redux Gene Wirchenko <genew@ocis.net> - 2011-06-26 20:42 -0700
          Re: Passing a Method Name to a Method, Redux markspace <-@.> - 2011-06-26 23:27 -0700
          Re: Passing a Method Name to a Method, Redux Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-06-27 03:04 -0400
            Re: Passing a Method Name to a Method, Redux Gene Wirchenko <genew@ocis.net> - 2011-06-27 13:12 -0700
              Re: Passing a Method Name to a Method, Redux Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-06-27 13:36 -0700
      Re: Passing a Method Name to a Method, Redux Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-06-24 15:50 -0700

csiph-web