I would guess that you have not made any measurements to support
or refute your guess ... Hint, hint.
OK, done the footwork.. created an XString class with the same fields
and equals logic as String. Then ran the profiling benchmark (below)
with the first if in place, and without. Here are the results:
With if check:
XString: 2041.559302 String: 2072.287253
XString: 2090.282933 String: 2076.000323
XString: 2092.470786 String: 2074.713913
XString: 2092.733079 String: 2073.96601
XString: 2092.218692 String: 2074.628331
XString: 2091.529936 String: 2072.115415
XString: 2092.015061 String: 2075.19818
XString: 2092.343548 String: 2073.740385
XString: 2092.733192 String: 2073.502145
XString: 2092.513025 String: 2074.677844
Without if check:
XString: 2052.875498 String: 2084.60745
XString: 2077.818231 String: 2080.210148
XString: 2080.658763 String: 2085.940975
XString: 2077.86366 String: 2086.251417
XString: 2074.737521 String: 2087.026064
XString: 2082.000822 String: 2087.007095
XString: 2080.464209 String: 2089.209287
XString: 2082.935433 String: 2089.499631
XString: 2078.67812 String: 2090.466871
XString: 2079.904874 String: 2091.204318
The timings show that the initial if does waste some time, but it's an
almost trivial amount. The test class includes in the times some
ArrayList.indexOf() overhead.. but even if this was factored out, the
gain would still be trivial.
package org.lv.tests;
import static org.lv.lego.constants.NumericConstants.MILLION;
import org.lv.lego.math.random.RandomKit;
import java.util.ArrayList;
import java.util.List;
public class XString {
private static final int NUM_LINES = (int) (0.05 * MILLION);
private static final int NUM_SEARCHES = 5000;
private static final int NUM_TESTS = 10;
private int count;
private char[] value;
private int offset;
XString(char[] chars) {
value = chars;
count = chars.length;
}
public boolean equals(Object anObject) {
// if (this == anObject) {
// return true;
// }
if (anObject instanceof XString) {
XString anotherString = (XString) anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}
public static void main(String[] args) {
System.out.println("Generating random Strings..");
List<String> lines = new ArrayList<String>();
List<XString> xlines = new ArrayList<XString>();
String lastLine = null;
XString lastXLine = null;
for (int i = 0; i < NUM_LINES; i++) {
String randomLineString = RandomKit.randomLineOfText();
XString randomLine = new
XString(randomLineString.toCharArray());
xlines.add(randomLine);
lines.add(randomLineString);
lastLine = randomLineString;
lastXLine = randomLine;
}
System.out.println("Done. Searching through them..");
for (int test = 0; test < NUM_TESTS; test++) {
long then = System.nanoTime();
for (int i = 0; i < NUM_SEARCHES; i++) {
int indexOf = xlines.indexOf(lastXLine);
}
long now = System.nanoTime();
long elapsed = now - then;
System.out.print("XString: ");
System.out.print(((double) elapsed) / MILLION);
System.out.print(" ");
then = System.nanoTime();
for (int i = 0; i < NUM_SEARCHES; i++) {
int indexOf = lines.indexOf(lastLine);
}
now = System.nanoTime();
elapsed = now - then;
System.out.print("String: ");
System.out.println(((double) elapsed) / MILLION);
}
}
}