2

I'm trying to test if switch statements execute faster than conditionals. The only thing is this is timing quantity instead of actual faster performance. What could I do to test the performance instead of simple quantity race tests?

class Statements {
    public static void main(String args[]) {
        int x = 0, y = 0, x1 = 0, y1 = 0;
        double startTime = System.currentTimeMillis();
        do {
            switch (x) {
                case 1:
                    x--;
                    y++;
                    break;
                default:
                    x++;
                    break;
            }
        } while (y < 1000000000);  //5.718, 6.736, 6.124
        double endTime = System.currentTimeMillis();
        System.out.println((endTime - startTime) / 1000 + " seconds");
           double startTime1 = System.currentTimeMillis();
        do {
            if (x1 < 1) {
                y1++;
                x1++;
            } else {
                x1--;
            }
        } while (y1 < 1000000000);
        double endTime1 = System.currentTimeMillis();
        System.out.println((endTime1 - startTime1) / 1000 + " seconds");
    }
}
Kyle
  • 3,004
  • 15
  • 52
  • 79

5 Answers5

6

No, it's not. There are many, many things that you can do wrong with micro-benchmarks, especially in Java.

This Stackoverflow question goes into the most important points.

Community
  • 1
  • 1
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
3

Actually, the first thing I would do would be to examine the generated assembly code (or, in your case, Java byte code).

You may well find that the same code is generated in both cases. Compilers are much smarter than they used to be and things like gcc at what I like to call "insane optimisation level" still produce code that shocks and awes me.

There are a lot of different things that can affect empirical measurements that will not affect a static analysis of the generated code. At a bare minimum, you should use statistical methods to minimise the impact of these variations.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • How can I read the assembly code? And there's a gcc compiler for java code? I thought it was only for C haha. I need to find out how to use the optimization levels. – Kyle Feb 01 '11 at 08:25
  • Well, there _is_ actually a gcc java compiler called gcj, but I was thinking more along the lines of analysis in general: examining the assembly for native compilers (such as with gcc -S), examining the bytecode in the class files for Java (with javap) and so on. – paxdiablo Feb 01 '11 at 09:19
3

If you're really interested in the performance of these tight loops, then have you tried looking at the resultant .class files and inspecting the JVM bytecode to give you an indication of the differences between them ?

I would be surprised to:

  1. find significant differences in the code
  2. find significant differences in the performance

javap will show you the bytecode.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
2

I'd recommend not to worry about it until you know you have an actual performance bottleneck. Premature optimization IS the root of all evil and you're way better of by keeping your code clean, readable and maintainable so you can focus on the actual bottlenecks if and when they emerge.

Mikko Wilkman
  • 1,495
  • 10
  • 8
0

You are measuring performance, as defined by number of loops/second for each case. If you're trying to see what's "faster", I'd be hard pressed to think of a different test to run.

(You may also want to try this test with the compiler's optimizer on and off, or at different levels. That's where it can get interesting.)