Zero multiplication

This is a discussion on Zero multiplication within the C Programming forums, part of the General Programming Boards category; I have this code which runs a lot of times: Code: int z = 10; int x = b * ...

1. Zero multiplication

I have this code which runs a lot of times:

Code:
```int z = 10;
int x = b * z;```
I know my program assing b = 0 about 35% of times. Does the CPU or compiled code runs into a complete multiplication routines, or does it returns 0 fast?

I ask this because I dont know if for speed optimization is better to put this code:

Code:
```int z = 10;
if (b) x = b*z;```
what is faster?

thanks
FS

2. Optimization is a good thing to think about, but I doubt the cost of an "if" is much different than the cost of multiplying by zero. In fact, the multiplication might be cheaper, speed wise.

I don't know assembly language and cannot claim this as fact, but the next couple of posters seem to agree.

3. Originally Posted by Kempelen
I have this code which runs a lot of times:

Code:
```int z = 10;
int x = b * z;```
I know my program assing b = 0 about 35% of times. Does the CPU or compiled code runs into a complete multiplication routines, or does it returns 0 fast?

I ask this because I dont know if for speed optimization is better to put this code:

Code:
```int z = 10;
if (b) x = b*z;```
what is faster?

thanks
FS
In my opinion speedwise extra time will be spent on checking the condtion of the if statement and thus 'if' will lead to a slower code.

4. Originally Posted by web
Premature optimization is the root cause of all evil.
So make sure you do all your optimizations stuff at the end.

5. stevesmithx, right on!

Oy - with the "which is faster" on a construct that resolves to a few assembler instructions....

Yes, the "if" test would make all multiplications slower, but consider this:

On modern hardware you'll see tens or hundreds of millions of multiplications go by in a second. The "if" will be nearly as fast.

What could you be doing that needs to consider the difference between a hundred millionth of a second or two? (I know the answer BTW).

You will want to understand the performance implications of what you write, no doubt about that (or you wouldn't be using C).

On the other hand, you need to learn the critical mass before any of that level of thinking is going to make much sense.

In your future it will probably be more important to know how to make matrix multiplications fast, or how to rip through a large buffer quickly - and you will.

6. Believe me - your CPU is probably faster than you'll ever be. Or it wouldn't be sold anymore. And we're talking about micro-to-nanoseconds. That won' really matter (unless you're writing a hi-speed program). First finish your program - then try this. You know about premature optimization, huh? ;-) And a single optimization switch is more worthy than most efforts put into this.

7. Originally Posted by stevesmithx
So make sure you do all your optimizations stuff at the end.
I suggest reading this article, and those it cites, for a more balanced view: Mature Optimization.

8. Well, I think I'm missunderstood. Any CPU industry wanting to make money optimizes, optimizes and lowers the prices. I think they would be smart enough to make multiplication by one or zero possible in constant time (if it turns out not really to affect other numbers). And it's true that you first should optimize your code in another way maybe. And your question is machine-specific: On machines with expensive branching, I'd leave everything as is. Else, it might save you some nanoseconds per multiplication.

@laserlight: Thanks for the enlightenment ;-D

9. That "if" does not matter because of branch prediction and speculative execution. On some modern processors the code will actually take both paths at once while the comparison is still pipelined. On the other hand, a modern processor might also optimize explicitly for multiplying by 0, 1, and maybe even 2.

I wouldn't be surprised if some architectures might benefit from an explicit check for zero before multiplying. Try it and find out.

10. I think some other architectures first check for zero.

11. If it's a modern processor, it will not be beneficial to skip multiplies by zero - I just wrote a small program to benchmark it. The checking version is about twice as slow.

Obviously, different processors do this more or less well. But on any recent processor, a mul instruction will be about the same number of clock-cycles as a branch.

--
Mats

12. Even if using the if-statement did improve performance for the multiply by 0 case, that is only 35% of your data, do you'd be adding an extra if-statement for the other 65% of the cases and slowing them down.

13. Originally Posted by cpjust
Even if using the if-statement did improve performance for the multiply by 0 case, that is only 35% of your data, do you'd be adding an extra if-statement for the other 65% of the cases and slowing them down.
Sure. So you really need to have a SLOW multiplier on the machine for there to be a benefit. And the smaller the proportion of zeros there are, the slower the multiplier has to be. A modern processor does a multiply in a couple of cycles (3-7, depending on mode, on the latest Athlon for example). I'd expect Intel's timing to be within a cycle or two of that timing. To compare the value, it will take at least two instructions: a compare (or test) instruction, and a conditional jump instruction. Whilst the ideal case is that this takes 2 clock-cycles, it is highly likely that the branch prediction isn't ALWAYS accurate, so it will sometimes have to refetch the code itself.

And of course, making the loop itself more complex means that the optimizer will have a harder time to make "perfect" code.

--
Mats

14. Originally Posted by laserlight
I suggest reading this article, and those it cites, for a more balanced view: Mature Optimization.
Darn!. Every time i learn some software philosophy, somebody always has something to say against it!.
Seriously though, thanks for the link dude.

Popular pages Recent additions