Just wondering if anyone knew off the top of their heads if there was much difference in doing the following:
String wibble = "<blah> blah blah </blah>.... <wibble> blah wibble blah </wibble> some more test here";
int i = wibble.lastIndexOf(">");
int j = wibble.lastIndexOf('>');
You can certainly make a string equal a single character if you wish, it takes more overheard from resources but, it's certainly do able, this would unlock many methods on that string as well.
The lastIndexOf() method returns the position of the last occurrence of specified character(s) in a string.
The indexOf() and lastIndexOf() function return a numeric index that indicates the starting position of a given substring in the specified metadata string: indexOf() returns the index for the first occurrence of the substring. lastIndexOf() returns the index for the last occurrence of the substring.
lastIndexOf() The lastIndexOf() method, given one argument: a substring to search for, searches the entire calling string, and returns the index of the last occurrence of the specified substring.
Opinions are great but data are better. I wrote a quick benchmark:
public static void main(String[] args)
{
System.out.println("Starting perfo test");
final long NUM_TESTS = 100000000L;
String wibble = "<blah> blah blah </blah>.... <wibble>"
+ " blah wibble blah </wibble> some more test here";
int x = -1;
Stopwatch sw = new Stopwatch();
System.out.println("--perfo test with " + NUM_TESTS + " iterations--");
sw.start();
for(long i = 0; i < NUM_TESTS; i++)
x = wibble.lastIndexOf(">");
sw.stop();
System.out.println("String first pass: " + sw + " seconds");
sw.start();
for(long i = 0; i < NUM_TESTS; i++)
x = wibble.lastIndexOf('>');
sw.stop();
System.out.println("Char first pass: " + sw + " seconds");
sw.start();
for(long i = 0; i < NUM_TESTS; i++)
x = wibble.lastIndexOf('>');
sw.stop();
System.out.println("Char second pass: " + sw + " seconds");
sw.start();
for(long i = 0; i < NUM_TESTS; i++)
x = wibble.lastIndexOf(">");
sw.stop();
System.out.println("String second pass: " + sw + " seconds");
//Compiler warning said x was never read locally.. this is to
//ensure the compiler doesn't optimize "x" away..
System.out.println(x);
}
Starting perfo test --perfo test with 100000000 iterations-- String first pass: 8.750 seconds Char first pass: 6.500 seconds Char second pass: 6.437 seconds String second pass: 8.610 seconds 63
The version with a char is about 25% faster, but both versions execute very quickly so it probably won't ever be a bottleneck in your code.
You can actually look into the source code of String class. It seems lastIndex(char) and lastIndex(String) were actually written separately. There should also be an overhead with String class. So char version might be a little bit faster, but I doubt there will be a case with any significant performance difference.
http://www.docjar.com/html/api/java/lang/String.java.html
Performance-wise, there might be a slight difference. For searching a single character, it's faster or equal to use char
instead of String
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With