2012-09-29 88 views
1

在Java中,由於我有一個帶有5個分隔符的字符串,例如「abc; 123; 123; abc; 123」,通常是相同的字符串長度。我需要驗證每個項目,而不是太冗長。stringtokenizer java - performance

速度更快?

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()

+2

您確定這很重要嗎?代碼已被分析過,而這部分是瓶頸的位置? –

+0

只需要知道理論;-) – user1320651

+2

我對理論的理解是,我會建議避免浪費時間浪費這樣的微觀優化嘗試,而將重點放在更重要的問題上,比如爲什麼'String#split(...)'是一般推薦使用StringTokenizer(根據API)。 –

回答

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   

}

選項

選項1 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,因爲它們在理論上是較少字節代碼指令。

有人可以確認