Re: Passing a Method Name to a Method, Redux
On 6/27/2011 1:53 PM, Gene Wirchenko wrote:
I am an experienced programmer, but I am not so experienced with
Java. I am trying to remedy the latter.
Honestly, what you've done so far is pretty crazy. I figured you were a
2nd year student who got in over his head on a personal project, or a
homework problem. No sane programmer would try to search for characters
the way you are.
If you want to tell us what your actual experience is, it might help.
I'm guessing your experience isn't actually programming, maybe HTML and
Flash or something. But I don't want to get into an argument here so if
you don't want to tell us then it's ok to drop it.
On the confusion, I see that a number of people have
micro-optimised for my code. I am looking at a bigger picture.
It's hard to give you a big picture. Well, a couple have tried. JavaCC
or parboiled or similar compiler-compiler would help you the most. But
besides that you've given us only a very micro example. We can't do
anything else with the code other than micro-optimize.
And on my system using a StringBuilder instead of '+' yields a 200%
speed up. Times go from 22 seconds to around 6. I don't really call
that micro.
Of course not. The test code is just for dealing with
identifiers, and it is a simplified version to boot.
Part of the problem is that you aren't really testing those three search
routines. You're testing other things like string concatenation,
because those are dominating the running time of your tests.
I'd start over. Download an IDE like NetBeans. Start a new project
with a single class with just the search routines (shown below). Use
Tools -> Create Unit Tests. That will at least generate a saner
framework for you to put your testing. It does need testing, and you
can make a couple of tests do timing for you. I think it'll help you
think about the problem more clearly too, things were kinda messy in there.
class TimingTesting
{
static String cParseString =
"//identifier//IDENTIFIER//a_b_c "
+"abc1234b5%$__dbl;one;two;three;END";
static String IdentChars =
"0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "_"
+ "abcdefghijklmnopqrstuvwxyz"; // sorted order!
static SortedSet<Character> IdentCharsSet
= new TreeSet<Character>();
static int nRepetitions = 1000000;
// Just these three methods and no more!!!
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 );
}
}