-3

So suppose I have a number called num, if some condition A happens, then increase the number by 1, else decrease it.

I could write the code like this:

if (A) ++num;
else --num;

This piece of code could also be written without any if-else, but would involve more arithmetics (just simple basic arithmetics, no exponents, logarithms or anything that one would need a scientific calculator to do!) Something like this:

num = num * x + y;

The question is, would the first or the second block of code perform faster? (This is in Java, btw).

(Please don't say it wouldn't make any difference. This is just very 'rudimentary' version of what I'm trying to do. But suppose there were an extremely good profiler that could measure the difference in runtime between these two's, which one would be faster?)

Thanks

One Two Three
  • 22,327
  • 24
  • 73
  • 114
  • make your code first, then use a profiler to check if it has the enough performance that you want/need. – Luiggi Mendoza May 27 '12 at 07:49
  • 1
    They don't do the same thing at all. What's the point in comparing their performance? Is it more efficient to write to a database or to download a file? Well, if you need to download a file, the performance of writing to a database is irrelevant. – JB Nizet May 27 '12 at 07:50
  • can you explain what is x and y. – Chandra Sekhar May 27 '12 at 07:52
  • Most likely, it will vary depending on your hardware, your _exact_ JVM version, and the precise context. Don't assume that there are definitive answers to questions like these. (Depending on context and details, some JITs might be smart enough to make them compile to _exactly_ the same bytecode, in which case there could well be no difference _at all, no matter how good your profiler._) – Louis Wasserman May 27 '12 at 07:59
  • 2
    @JBNizet: I think what the OP is trying to say is that for the particular problem he's trying to solve, he can either compute +/- 1 with some bit twiddling and other arithmetic, or he can do an explicit `if` statement. For example, `if (x >= 0) x++; else x--;` is equivalent to `x += 1 | (x >> 31)`. He's asking if the arithmetic route will be faster. (To which the answer is, it depends on the arithmetic you'd need to do, the speed of hardware multiplication, the smartness of your JIT, and so on.) – Louis Wasserman May 27 '12 at 08:05
  • 3
    @LouisWasserman: Agreed, and agreed with your previous comment. But my point was that if he wants us to discuss the performance of two similar operations, we'd better know how these similar operations are implemented. Answering "which is faster? this if/else statement or some unknown arithmetic operation that would lead to the same result?" is plain impossible. – JB Nizet May 27 '12 at 08:13
  • It wouldn't make any difference. – Marko Topolnik May 27 '12 at 09:14
  • @LouisWasserman Yes, that's exactly what I mean (ie., conditional vs non-conditional instruction), thank you! Finally, there's someone to understand the question. – One Two Three May 28 '12 at 02:07

3 Answers3

4

These kind of optimizations doesn't make much sense and are evil most of the time, especially in the Java virtual machine, that have JIT and that optimize the bytecode.

Keep the code as more readable as you can, don't worry about this kind of optimization ...

aleroot
  • 71,077
  • 30
  • 176
  • 213
3

The answer is that it depends on the context, and the ability of the JIT compiler on your platform to optimize the code.

  • It probably won't make a noticeable difference to the performance of your application.

  • Even if it does, we can't predict what difference it will make, because it will depend on your platform (e.g. on the particular JVM / version you are using), and on the surrounding code.

The best advice is to write the code simply to give the optimizer the best chance to do its job. Then, if performance is a concern, profile your application and see where the hotspots are, and optimize the hotspots. Only optimize this code if the profiler says it is a hotspot.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

It is worth nothing that the cost of a branch can be 10x higher or more than an arithmetic instruction. When the same side of a branch is used repeatedly, the processor can reduce the cost to almost nothing, but when the side of the branch changes, this can cost in the order of 100 clock cycles. (Given a ++ or -- can be just one clock cycle this is significant)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130