Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.lastIndexOf() is there a difference between a char and a string with a single character?

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('>');
like image 303
Omar Kooheji Avatar asked Jan 22 '09 15:01

Omar Kooheji


People also ask

Can a single character be a string?

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.

What does the string method lastIndexOf string returns?

The lastIndexOf() method returns the position of the last occurrence of specified character(s) in a string.

What is the difference between the string methods indexOf and lastIndexOf )?

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.

What does lastIndexOf mean in Javascript?

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.


3 Answers

Opinions are great but data are better. I wrote a quick benchmark:

Test Code

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); 
}

Output

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

Conclusion

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.

like image 177
Kip Avatar answered Nov 03 '22 07:11

Kip


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

like image 39
dasony Avatar answered Nov 03 '22 05:11

dasony


Performance-wise, there might be a slight difference. For searching a single character, it's faster or equal to use char instead of String.

like image 25
mmx Avatar answered Nov 03 '22 07:11

mmx