Jesper said:
Four optimizations:
1. Removed the "address.length() > 0" expression.
That's an acceptable optimization, since the next condition will fail just
as easily.
2. Used char instead of String when searching for characters.
The code I posted wasn't c-n-p from our source tree but just what I typed
in. Our real code uses char searches.
3. Search for the '.' only on characters after the '@'. You search the
whole address.
Our search looks for the last index of '.' to compare to the index for '@'.
4. Store the result of 'address.indexOf('@')' in a temporary variable.
I avoid creating local variables that aren't used beyond a single line.
Unless significant optimization is achieved, I wouldn't do that for
maintainability.
Maybe you should check your facts before posting.
At this point, it's been speculation.
Just to make sure I
benchmarked both algorithms on 1000 random addresses. Here are my
results:
j2sdk1.4.1_05\bin\javaw.exe:
My algorithm: 2414 ms
Your algorithm: 3806 ms
So one too 2ms each and the other took 3ms? I ran my algorithm versus yours
with 100,000 reps for the address "(e-mail address removed)" and got a wide
range of results. Sometimes yours was faster, sometimes mine was faster.
Both processed the address in less than a millisecond The source code (open
to your correction) is:
---8<[snip]---
public class Test
{
private static final long REPETITIONS = 1000000L;
public static long myWay(String address)
{
long time = System.currentTimeMillis();
boolean pass = false;
for(long reps = 0;reps < REPETITIONS;reps++)
{
if((address != null &&
address.length() > 0 &&
(address.indexOf('@') > 0 &&
address.lastIndexOf('.') > address.indexOf('@'))))
{
pass = true;
}
}
time = System.currentTimeMillis() - time;
return time;
}
public static long hisWay(String address)
{
long time = System.currentTimeMillis();
boolean pass = false;
for(long reps = 0;reps < REPETITIONS;reps++)
{
if(address != null)
{
int index = address.indexOf('@');
if(index > 0 &&
address.indexOf('.',index + 1) > 0)
{
pass = true;
}
}
}
time = System.currentTimeMillis() - time;
return time;
}
public static void main(String[] args)
{
for(int run = 0;run < 2;run++)
{
long mine = myWay("(e-mail address removed)");
long his = hisWay("(e-mail address removed)");
if(run > 0)
{
System.out.println(" My way: " + mine + " [" + (mine / REPETITIONS) + "ms
each]");
System.out.println("His way: " + his + " [" + (his / REPETITIONS) + "ms
each]");
}
}
}
}
---8<[snip]---
Your algorithm and mine came very close to each other, processing 1,000,000
entries in < 150ms, giving each one about 0ms processing time. I don't know
why you had such large processing times unless you're including your random
address generation times in the processing times, which would be unfair to
the algorithm since it includes non-algo time in their results. With my
test, your algorithm is no more effecient than mine.
To quote your post, "[m]aybe you should check your facts before posting."
Care to post your code?
j2sdk1.4.1_05\bin\javaw.exe -server:
My algorithm: 1232 ms
Your algorithm: 3706 ms
So, it seems in a good JVM, like Hotspot server, your code is much
easier to optimize than mine (not) ;-) Feel free to perform your own
benchmarks, but I doubt you'll get different results.
Your doubts would be unfounded, then.