> Have you measured the overhead when using regex in
> this case?
Can't be bothered - but the most efficient way regex could work on it would be to check the value range of each char, once. Which is what the "manual" solution does as well. Now considering that patterns need to be compiled before used, this compilation time can be considered overhead, although, admittedly, it will hardly matter if it's a one-time action on user input.
Which solution would be the best one IMO strongly depends on the allowed value range and the further use of that String, which the OP never specified.
> > sabre> it's true that parseInt won't take into
> > considerations all kind of number, that's why i
> also
> > suggested regex
>
> You edited your post after I quoted it!
actually i was editing it while you posted your answer, i think
i'm not sure it's possible to edit your own post once someone else has answered behind :)
> > > sabre> it's true that parseInt won't take into
> > > considerations all kind of number, that's why i
> > also
> > > suggested regex
> >
> > You edited your post after I quoted it!
>
> actually i was editing it while you posted your
> answer, i think
>
> i'm not sure it's possible to edit your own post once
> someone else has answered behind :)
I had 'quoted' it and was in the process of answering it!
> > > > sabre> it's true that parseInt won't take into
> > > > considerations all kind of number, that's why
> i
> > > also
> > > > suggested regex
> > >
> > > You edited your post after I quoted it!
> >
> > actually i was editing it while you posted your
> > answer, i think
> >
> > i'm not sure it's possible to edit your own post
> once
> > someone else has answered behind :)
>
> I had 'quoted' it and was in the process of answering
> it!
no harm done ^^
by the way , looks like OP found what he needed since he isn't saying anything
> > Have you measured the overhead when using regex in
> > this case?
>
> Can't be bothered - but the most efficient way regex
> could work on it would be to check the value range of
> each char, once. Which is what the "manual" solution
> does as well. Now considering that patterns need to
> be compiled before used, this compilation time can be
> considered overhead, although, admittedly, it will
> hardly matter if it's a one-time action on user
> input.
>
> Which solution would be the best one IMO strongly
> depends on the allowed value range and the further
> use of that String, which the OP never specified.
I have just compared the two approaches. On my average machine using JDK1.6 using test code
public static void testUsingRegex() throws Exception
{
java.util.regex.Matcher m = java.util.regex.Pattern.compile("\\d+").matcher("0");
String[] targets = {"1234","12sx"};
long start = 0;
for (int i = -1000; i < NUMBER_OF_ITERATIONS; i++)
{
if (i == 0)
start = System.currentTimeMillis();
for (String target : targets)
{
m.reset(target);
boolean valid = m.matches();
}
}
long end = System.currentTimeMillis();
double delta = 1000.0 * (end-start) / NUMBER_OF_ITERATIONS;
System.out.println("Regex " + delta + " us");
}
public static void testUsingCharArray() throws Exception
{
java.util.regex.Matcher m = java.util.regex.Pattern.compile("\\d+").matcher("0");
String[] targets = {"1234","12sx"};
long start = 0;
for (int i = -1000; i < NUMBER_OF_ITERATIONS; i++)
{
if (i == 0)
start = System.currentTimeMillis();
for (String target : targets)
{
boolean valid = true;
char[] chars = target.toCharArray();
for (char ch : chars)
{
if (!Character.isDigit(ch))
{
valid = false;
break;
}
}
}
}
long end = System.currentTimeMillis();
double delta = 1000.0 * (end-start) / NUMBER_OF_ITERATIONS;
System.out.println("Char array " + delta + " us");
}
I get as result
Regex 0.44248 us
Char array 0.25672 us
averaged over 100,000,000 iterations.
So yes, as expected, converting to a char array is faster but considering the actual values and the simplicity of the regex code I know which I prefer.
Message was edited by:
sabre150
Go the regex, it's more flexible... but, I'm working on a system with 21 million features... and trust me, such micro-optimisations can really add up... so it's horses for courses... the hard bit I've found is indentifying the bottlenecks not (usually) resolving them.
total time on 21,000,000
Regex 14516
Manual 2750
It's also interesting to see what happens when you make the target strings progressively longer... regex doesn't slow down (proportionatly) as much the char array (I wonder why?)... but it's still 14 seconds faster.
Regex 17485
Manual 3359
I'd use a regex unless/until I had proof that it's too slow. Performance shouldn't ever be the primary design consideration, but it should always be a secondary one.
Keith.
'corlettk' - you need to show how you use the regex to get your timings because they do not stack up with my results.
The only way I can get a timing ratio comparable with yours is if I use boolean valid = target.matches("\\d+");
which is definitely not the way to use a regex when you want to compare performance.
Message was edited by:
sabre150
> > try to parse it with Integer.parseInt(String str)
> >
> > if it throws a "numberformatexception" string does
> > not contain only digits ; if not, it contains only
> > digits
>
> -123 ?
It wasn't clear to me whether the OP means "only contains digits" or "contains a valid number" or "contains a valid integer".