Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Regex vs. String[]  RSS feed

 
Jacob Whitten
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

was wondering if someone could help me decide what to use better..
I want to know if I should use white or black foreground for my digital sticky note project based on color swatches. I use a string[] in my method like so below. Is it more effecient and/or faster to use regex for this. Thanks any help is welcom.

 
Sylven Yip
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think reg-exp is better.
if you use String[],you must store a String array and check them in a circle.

regex only need a Sting object.i suppose it is more effective.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, but only needing one String hardly is an argument for it being faster. The question is how the regex is executed. The most reliable way to find out which is faster is to try it.

There is a method that will very likely be faster and also is more elegant, though: put all the Strings in a HashSet and test whether the name is contained in the set using the contains method. This is supposed to have a performance of O(1) - that is, the performance doesn't depend on how many Strings are in the Set. But even here you need to test it to be sure - for a small number of Strings a loop over an array might still be faster.

And most importantly, you should only worry about this if you are sure that this code in fact is a performance bottleneck for your system. As long as you haven't proven that it's problematic performance wise, you simply should implement it in an elegant way.
 
Jacob Whitten
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to y'all
i suppose with a small number of strings it doesn't matter

but thanks for the suggestions
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have another suggestion. In your original code, there's no reason that final String[] c needs to be defined inside the translate() method. That means a new String[] array will be created and initialized every time the method is run. Since the array never changes, there's no need to do that. Instead, define the variable outside the method, as a private static final member variable. That way you just create it and initialize it once.

Similarly, if you use a regular expression, you can define a Pattern variable for it - and again, that Pattern can be a private final static variable, initialized only once. Or if you use a HashSet, that can be held in a private static final variable, initialized only once.

Personally I would just use the HashSet, as all three methods are approximately equal in length & complexity, but the HashSet is likely to be fastest. If it turns out later that performance needs improvement in this area, then I'd test alternatives. But my gut tells me to try the HashSet first.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!