|
The third run - problems with regular expressionsWhen I track this code down in the application I find some innocent code that looks like this:
if ( (aMatcher.find()) || (subjectField.indexOf(keywordToReject)>=0) )
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.
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.
Next: Getting harder now Up: Optimizing your first project Previous: The second run |