devdaily home | java | perl | unix | directory | weblog

up previous next
Next: Getting harder now Up: Optimizing your first project Previous: The second run

The third run - problems with regular expressions

When I track this code down in the application I find some innocent code that looks like this:

if ( (aMatcher.find()) || (subjectField.indexOf(keywordToReject)>=0) ) 

  markMessageAsRejected(emailMessage, ``Subject field matched a keyword.''); return; 
}

As simple as this code looks, there are a couple of problems here. First, I'm searching for a regular expression before doing a faster indexOf operation. Therefore, I should probably reverse the order of these calls. But an even bigger problem is that I've used a slow method in the Matcher class. I'll save you a bunch of research time and just tell you the secret: the find() method is much slower than the matches() method in the Matcher class. It turns out that the find() method is doing a lot more work than you might expect, so although I thought this was the right method when I selected it, a little more research told me to switch my find() call to a matches() call. 

So, after making these two changes, I ran Optimizeit for a third time. The results are shown in the next figure.

Woo-hoo, this cut the run time down to 9,956 ms, and I haven't even broken a sweat yet. Let's keep pushing by drilling down into the code with Optimizeit.

Yikes. As you can see from the screen above, it just suddenly got a lot harder. I've highlighted another line that's an easy win for me; here I can probably replace find() with matches() again and cut down that 506ms number. But the rest doesn't look too easy. A lot of the remaining code is not something I can change too easily. 

First things first though. Let me go for another easy win by getting rid of that find() call. Actually, when I look at the code I find several other instances where I'm calling find() instead of matches(), so I change them all. The result of this is that I reduce the run time to 9,548 ms, as shown in the next figure.

As I dig further into the code I see the results that are shown in the next figure. 


up previous next
Next: Getting harder now Up: Optimizing your first project Previous: The second run