Performance of the string switch statement

One of the new Java 7 features is possibility to use String values in switch statements. Proper usage of this can be found for example in oracle tutorials.

But what i wanted to know is how fast is that. Is the new way faster, than a series of if-else statements, or is it slower?

Tests

I have prepared two simple tests. The first one uses String values of very different content and the second one uses a set of very similar values. The rest of the class remains the same.

First test

Java7Switch test = new Java7Switch();
// Java6If test = new Java6If();

long elapsed = 0;
for (int j = 0; j < 10; j++) {
 long before = System.currentTimeMillis();

 long variable = 0;
 for (int i = 0; i < 10000000; i++) {
  int tex = 0;
  tex = test.monthNameToDays("December1");
  tex = test.monthNameToDays("December2");
  tex = test.monthNameToDays("December3");
  tex = test.monthNameToDays("December4");
  tex = test.monthNameToDays("December5");
  tex = test.monthNameToDays("December6");
  tex = test.monthNameToDays("December7");
  tex = test.monthNameToDays("December8");
  tex = test.monthNameToDays("December9");
  tex = test.monthNameToDays("December10");
  tex = test.monthNameToDays("December");
  variable += tex;
 }

 long after = System.currentTimeMillis();

 elapsed += after - before;
 System.out.println("variable: " + variable);
 System.out.println("Time elapsed: " + (after - before));
}
System.out.println("Averange time: " + (elapsed / 10)); 

As you can see, the test runs in ten iterations for more predicative results. Than the full loop is executed ten million times for the measurement in milliseconds to be possible and accurate.

 Second data

  tex = test.monthNameToDays("January");
  tex = test.monthNameToDays("February");
  tex = test.monthNameToDays("March");
  tex = test.monthNameToDays("May");
  tex = test.monthNameToDays("July");
  tex = test.monthNameToDays("August");
  tex = test.monthNameToDays("April");
  tex = test.monthNameToDays("June");
  tex = test.monthNameToDays("September");
  tex = test.monthNameToDays("November");
  tex = test.monthNameToDays("December");

Second data set contains very different values, enabling equals method to evaluate the difference on the first character (in some cases later).

Java7Switch class

public class Java7Switch {
 public int monthNameToDays(String s) {
  switch (s) {
  case "December1":
   return 30;
  case "December2":
   return 31;
  case "December3":
   return 32;
  case "December4":
   return 29;
  case "December5":
   return 22;
  case "December6":
   return 23;
  case "December7":
   return 25;
  case "December8":
   return 26;
  case "December9":
   return 27;
  case "December10":
   return 35;
  default:
   return 28;
  }
 }
}

Java6If class

public class Java6If {
 public int monthNameToDays(String s) {
  if (s.equals("December1"))
   return 30;
  else if (s.equals("December2"))
   return 31;
  else if (s.equals("December3"))
   return 32;
  else if (s.equals("December4"))
   return 29;
  else if (s.equals("December5"))
   return 22;
  else if (s.equals("December6"))
   return 23;
  else if (s.equals("December7"))
   return 25;
  else if (s.equals("December8"))
   return 26;
  else if (s.equals("December9"))
   return 27;
  else if (s.equals("December10"))
   return 35;
  else
   return 28;
 }
}

Switch and if classes are very similar for the second set of values, so I won't copy them here.

Results

First data set results are:
Switch - Average time: 2261 ms
If - Average time: 24262 ms


Second data set results are:
Switch - Average time: 2246 ms
If - Average time: 8784 ms



Conclusion

I was quite surprised when I saw the results. When dealing with similar values, the switch statement is more than ten times faster. The difference between methods is significantly smaller when dealing with varied values, but the switch method remains far more efficient (four times).
I admit, that  the results are affected by my selection of values and its count. On second note, these constructions cannot be considered clean code and should be avoided if possible.
Although this test may be oversimplified, it shows the fact, that this new Java 7 feature is a better alternative, than several if statements.

Comments

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training India . Nowadays Java has tons of job opportunities on various vertical industry.

    ReplyDelete

Post a Comment

Popular posts from this blog

Automatic jsp recompile on Jboss AS 7

Ldap security for Jenkins CI

Wicket CDI integration on Jboss AS 7