2
\$\begingroup\$

This is the relevant piece of my code (false is returned if the whole cycle is finished, pattern is a String passed to the function):

for (FileLine fileLine : fileLines) {
 itemText = fileLine.getText();
 itemStrings = new ArrayList<String>();
 itemStrings.addAll(Arrays.asList((itemText).split(" ")));
 for (String itemString : itemStrings) {
 if (itemString.equalsIgnoreCase(pattern)) {
 return true;
 }
 }
}

How can I speed this up? I've tried using regex, something like:

//before the for cycle
Pattern regexPattern = Pattern.compile(pattern);
//in the cycle
if (regexPattern.matcher(itemText).matches())
 return true;

But it doesn't work for some reason. Maybe I'm doing it wrong, or there's a completely different way of accomplishing this?

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Jul 9, 2013 at 13:46
\$\endgroup\$
0

2 Answers 2

2
\$\begingroup\$

You should probably be using find() instead of matches() (source).

I would recommend using regexpal to test out your pattern, just in case.

Whether or not the Regex solution is faster, you'd have to run some tests, but I suspect it's MUCH faster.

answered Jul 9, 2013 at 14:42
\$\endgroup\$
1
\$\begingroup\$

Why are you creating a new ArrayList each time, then filling it with (read: basically copying each element of) a list you created from an array? You have no need right here for the dynamic-ness of a List.

Why not skip the two extraneous creations (and the resulting GC work, if this is a big file), and work with the array directly...and only create the ArrayList once you find a match?

for (FileLine fileLine : fileLines) {
 itemText = fileLine.getText();
 String[] candidate = itemText.split(" ");
 for (String itemString : candidate) {
 if (itemString.equalsIgnoreCase(pattern)) {
 // Note: if `itemStrings` is local, you don't even have to create it.
 // This code is only necessary if it's an instance or class variable.
 itemStrings = new ArrayList<String>();
 itemStrings.addAll(Arrays.asList(candidate));
 return true;
 }
 }
}

The biggest difference here is that if you don't find a match, itemStrings never gets set -- whereas with the original code, it'll be set to the last line, regardless of whether that line was a match.

answered Jul 9, 2013 at 17:48
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.