当前位置:  开发笔记 > 编程语言 > 正文

String.lastIndexOf()char和具有单个字符的字符串之间有区别吗?

如何解决《String.lastIndexOf()char和具有单个字符的字符串之间有区别吗?》经验,为你挑选了1个好方法。

只是想知道是否有人知道如果在执行以下操作方面存在很大差异,那么他们是否知道他们的头脑:

String wibble = " blah blah ....  blah wibble blah  some more test here";

int i = wibble.lastIndexOf(">");
int j = wibble.lastIndexOf('>');

Kip.. 12

意见很好,但数据更好.我写了一个快速基准:

测试代码

public static void main(String[] args)
{
  System.out.println("Starting perfo test");

  final long NUM_TESTS = 100000000L;

  String wibble = " blah blah .... "
                + " blah wibble blah  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

结论

带有char的版本速度提高了大约25%,但两个版本的执行速度都非常快,因此它可能永远不会成为代码中的瓶颈.



1> Kip..:

意见很好,但数据更好.我写了一个快速基准:

测试代码

public static void main(String[] args)
{
  System.out.println("Starting perfo test");

  final long NUM_TESTS = 100000000L;

  String wibble = " blah blah .... "
                + " blah wibble blah  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

结论

带有char的版本速度提高了大约25%,但两个版本的执行速度都非常快,因此它可能永远不会成为代码中的瓶颈.

推荐阅读
sx-March23
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有