Boolean expressions optimizations in Java?

It doesn't do that because expensiveComputation() may have side effects which change the state of the program. This means that the order in which the expressions in the boolean statements are evaluated (expensiveComputation() and b) matters. You wouldn't want the compiler optimizing a bug into your compiled program, would you?

For example, what if the code was like this public static boolean expensiveComputation() { for (int I = 0; I.

This is a silly example - its somewhat different with an ||. Why not just put the side-effect into expensiveComputation, that would illustrate the point well. – Paul Biggar Aug 28 '09 at 18:55 3 It's not really different with an ||.

The same kind of optimization could be done, and it has the same implications. This was just the first example I thought of that has an immediately noticable effect, and that was related to his example. – Sean Nyman Aug 28 '09 at 19:22.

Because expensiveComputation() may have side-effects. Since Java doesn't aim to be a functionally pure language, it doesn't inhibit programmers from writing methods that have side-effects. Thus there probably isn't a lot of value in the compiler analyzing for functional purity.

And then, optimizations like you posit are unlikely to be very valuable in practice, as expensiveComputation() would usually be required to executed anyway, to get the side effects. Of course, for a programmer, it's easy to put the be first if they expect it to be false and explicitly want to avoid the expensive computation.

Actually, some compilers can optimise programs like the one you suggested, it just has to make sure that the function has no side-effects. GCC has a compiler directive you can annotate a function with to show that it has no side-effects, which the compiler may then use when optimizing. Java may have something similar.

A classic example is for(ii = 0; strlen(s) > ii; ii++) which gets optimized to n = strlen(s); for(ii = 0; n > ii; ii++) by GCC with optimization level 2, at least on my machine.

The compiler will optimize this if you run the code often enough, probably by inlining the method and simplifying the resulting boolean expression (but most likely not by reordering the arguments of &&). You can benchmark this by timing a loop of say a million iterations of this code repeatedly. The first iteration or two are much slower than the following.

– Michael Foukarakis Aug 29 '09 at 9:19 Yes, the JVM contains an optimizing just-in-time compiler which compiles code that is used heavily. I did that benchmark, the first iteration takes 10ms, the second five, and after that it only shows zero time for each iteration. – starblue Aug 29 '09 at 9:51 @starblue: I don't think that the compiler is allowed to reorder the evaluation of the LHS & RHS expressions unless it can be proved that the outcome of the computation is never changed by the optimization.

– Stephen C Aug 29 '09 at 10:10 No, the compiler doesn't need to reorder. After inlining it will see the boolean constant and optimize the && away. – starblue Aug 29 '09 at 10:20 which doesn't avoid the expensiveCall – soru Aug 29 '09 at 19:13.

You certainly can't execute these instructions in Java. XOR, OR, AND, and NOT. Extra bitwise instructions.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions