字符串标记器性能



在Java中,假设我有一个有5个delims的字符串,例如"abc;123;123;abc;123",并且通常字符串长度相同。我需要验证每个项目,不要太冗长。

什么更快?

if (stoken.hasMoreTokens()) {
    final String test=stoken.nextToken();
} else {
    throw new ParseException("Some msg",0);
}

if (!stoken.hasMoreTokens()) 
    throw new ParseException("Some msg",0);
final String test=stoken.nextToken();

if (!stoken.hasMoreTokens()) {
    throw new ParseException("Some msg",0);
}
final String test=stoken.nextToken();

还是使用split()

所以有人可以确认以下内容吗

给出的选项 1

import java.text.ParseException;
import java.util.StringTokenizer;
public class stringtok
{ 
  public static void main(String[] argv)
  throws Exception
  {
      String data="ABC";
      final StringTokenizer stoken=new StringTokenizer(data.toString(),";");
      if (stoken.hasMoreTokens()) {
          final String test=stoken.nextToken();
      } else {
          throw new ParseException("Some msg",0);
      }
  }
} 

以字节码生成

Compiled from "stringtok.java"
public class stringtok {
public stringtok();
  Code:
     0: aload_0       
     1: invokespecial #1                  // Method java/lang/Object."<init>":()V
     4: return        
public static void main(java.lang.String[]) throws java.lang.Exception;
  Code:
     0: ldc           #2                  // String ABC
     2: astore_1      
     3: new           #3                  // class java/util/StringTokenizer
     6: dup           
     7: aload_1       
     8: invokevirtual #4                  // Method java/lang/String.toString:()Ljava/lang/String;
    11: ldc           #5                  // String ;
    13: invokespecial #6                  // Method java/util/StringTokenizer."<init>":(Ljava/lang/String;Ljava/lang/String;)V
    16: astore_2      
    17: aload_2       
    18: invokevirtual #7                  // Method java/util/StringTokenizer.hasMoreTokens:()Z
    21: ifeq          32
    24: aload_2       
    25: invokevirtual #8                  // Method java/util/StringTokenizer.nextToken:()Ljava/lang/String;
    28: astore_3      
    29: goto          43
    32: new           #9                  // class java/text/ParseException
    35: dup           
    36: ldc           #10                 // String Some msg
    38: iconst_0      
    39: invokespecial #11                 // Method java/text/ParseException."<init>":(Ljava/lang/String;I)V
    42: athrow        
    43: return        

}

给出选项 2 和 3(大括号是相同的字节码)

import java.text.ParseException;
import java.util.StringTokenizer;
public class stringtok2
{
    public static void main(String[] argv)
    throws Exception
    {
        String data="ABC";
        final StringTokenizer stoken=new StringTokenizer(data.toString(),";");
        if (!stoken.hasMoreTokens()) throw new ParseException("Some msg",0);
        final String test=stoken.nextToken();
    }
}

以字节码生成

Compiled from "stringtok2.java"
public class stringtok2 {
  public stringtok2();
    Code:
       0: aload_0       
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return        
  public static void main(java.lang.String[]) throws java.lang.Exception;
    Code:
       0: ldc           #2                  // String ABC
       2: astore_1      
       3: new           #3                  // class java/util/StringTokenizer
       6: dup           
       7: aload_1       
       8: invokevirtual #4                  // Method java/lang/String.toString:()Ljava/lang/String;
      11: ldc           #5                  // String ;
      13: invokespecial #6                  // Method java/util/StringTokenizer."<init>":(Ljava/lang/String;Ljava/lang/String;)V
      16: astore_2      
      17: aload_2       
      18: invokevirtual #7                  // Method java/util/StringTokenizer.hasMoreTokens:()Z
      21: ifne          35
      24: new           #8                  // class java/text/ParseException
      27: dup           
      28: ldc           #9                  // String Some msg
      30: iconst_0      
      31: invokespecial #10                 // Method java/text/ParseException."<init>":(Ljava/lang/String;I)V
      34: athrow        
      35: aload_2       
      36: invokevirtual #11                 // Method java/util/StringTokenizer.nextToken:()Ljava/lang/String;
      39: astore_3      
      40: return        
}

所以答案是选项 2 和 3,因为它们理论上是较少的字节码指令。

有人可以确认吗

相关内容

  • 没有找到相关文章

最新更新