Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: markspace <-@.> Newsgroups: comp.lang.java.programmer Subject: Re: Passing a Method Name to a Method, Redux Date: Fri, 24 Jun 2011 12:19:58 -0700 Organization: A noiseless patient Spider Lines: 58 Message-ID: References: <1lu707p0cpr9vhrpv51d7hmst6bt1qdbcv@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 24 Jun 2011 19:20:03 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="20GYBuezvHpp83lA3Rh62Q"; logging-data="22083"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+sxZBu4OcN7OhJl44jFBgZONHYzxQ8vh0=" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.18) Gecko/20110616 Thunderbird/3.1.11 In-Reply-To: Cancel-Lock: sha1:oSbE43SxFi45qTy/b9KEi9GTSrU= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5655 On 6/24/2011 11:45 AM, Gene Wirchenko wrote: > No. I expect that I will be using the resulting preprocessor for > years. The test code will be tossed shortly. You have a couple of problems with your code, one organizational and the other understanding the effeciencies. The organizational one relates to the idea that you'll just toss your tests away. Don't ever do that! The test code is part of the project, and should remain with it. Test code is also put under code control, and managed along with the projects. It's important because every time you want to change your parser, you'll need to re-run the tests to make sure everything is working. Are you using an IDE? Most will auto generate a test framework for you. It's very handy and you should be doing this regardless how you write code. The IDE just makes it very handy. > No. I am just after the timing. The other thing, efficiency, I'll show you right now. The organizational stuff is actually probably a bigger deal, but I think you'll be happy to see how to make code faster. This line here is the biggest offender. > cIdent += CurrChar; This is super inefficient inside a loop. To do this, the system has to create a new string with one extra character, and then toss away the old string. Making a new object and tossing an old one is bound to slow you down. final public void parse() { StringBuilder sb1 = new StringBuilder( 255 ); for( int xScan = 0; xScan < TimingTesting.cParseString.length(); xScan++ ) { char c = TimingTesting.cParseString.charAt( xScan ); if( find( c ) ) { sb1.append( c ); } } String ... = sb1.toString() Here's my adaptation of your loop. Notice I make a StringBuilder once, outside the loop, and call append() inside the loop, which is much much faster. Then I call toString once outside the loop again, so I only create a new String once, not each time inside the loop. Try to refactor your code to do this, it will make it much faster. One last thing for now: on splitting a string into tokens, look at this: String[] tok = TimingTesting.cParseString.split( "[^a-zA-Z0-9]+" ); System.out.println( Arrays.toString( tok ) );