1

In Java, given I have a string with 5 delims e.g. "abc;123;123;abc;123", and typically the same string length. I need to validate each item, not to be too verbose.

What's faster?

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

or

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

or

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

or use split()?

9
  • 2
    Are you sure that this matters, that the code has been profiled and this section is the location of a bottleneck? Commented Sep 29, 2012 at 18:39
  • 2
    My understanding of theory is that I would recommend avoiding time wasting micro-optimization attempts like this and would instead focus on more important issues, like why String#split(...) is recommended over StringTokenizer in general (as per the API). Commented Sep 29, 2012 at 18:43
  • understand that split is far slower and Im keen to know what difference it makes when im needing to shave precious milliseconds. I read up on both and got a good grip, I dont need to return an array, or worry about empty tokens. Commented Sep 29, 2012 at 18:52
  • slit uses regular expressions and these can be somewhat slow. Commented Sep 29, 2012 at 18:53
  • 3
    The three snippets do exactly the same thing. The presence or absence of curly braces doesn't make any difference, and whether you test for the nominal case or for the exceptional case doesn't matter at all. Choose what you find the most readable and maintainable. Commented Sep 29, 2012 at 19:24

1 Answer 1

1

So can someone confirm the following

Option 1 given

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

produces in bytecode

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        

}

Options 2 & 3 given (curly braces is identical bytecode)

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

}

produces in bytecode

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        
}

So the answer is Option 2 & 3 as they are theoretically less bytecode instructions.

can someone confirm

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.