0

I've just learned that additive operation is faster than multiplicative operation in C. Therefore, I'm curious whether (a+b)*c would be calculated faster than a*c+b*c ?

user438383
  • 5,716
  • 8
  • 28
  • 43
  • 3
    Just to complicate things: modern processors may do the 2 multiplications *in parallel*. – Scott Hunter May 16 '23 at 12:45
  • On what CPU? What compiler and compilation flags? Integer or floating point operations? – dimich May 16 '23 at 12:49
  • 2
    *I've just learned that additive operation is faster than multiplicative operation in C.* That might even be true. Then again, it might not. Saying something like "X is faster than Y" has almost no meaning when CPUs are deeply-pipelined devices that run multiple instructions in parallel. Don't worry about microoptimizations like this at all. You'll almost never have any real effect on processing speed, and any effect you have has a good chance of making things worse. – Andrew Henle May 16 '23 at 12:50
  • @ScottHunter Except there is just 1 multiplication once optimized :) – Lundin May 16 '23 at 12:50
  • 1
    @Lundin Pedantically, `a*c+b*c` better not be optimized to `(a+b)*c` ;-) – Andrew Henle May 16 '23 at 12:52
  • @AndrewHenle Not sure what you mean. That optimization is exactly what happened when I tried it on gcc. – Lundin May 16 '23 at 12:56
  • 2
    @Lundin `(a+b)*c` can produce different results than `a*c+b*c`. At least for floating-point calculations, such an optimization shouldn't be done. If the types were all unsigned integer types so overflow couldn't happen and UB invoked, the different results could also apply if the arguments were different sizes such as `uint8_t a,b; uint32_t c;`. Signed-integer operations? Since overflow invokes UB in that case, maybe the optimization is allowed? – Andrew Henle May 16 '23 at 13:04
  • 1
    @AndrewHenle Ah yeah I guess the OP didn't specify what type this is for, I just assumed plain old integer. Floating point is another story entirely though, check out [this oddity](https://stackoverflow.com/questions/73985098/clang-14-0-0-floating-point-optimizations) as one example. – Lundin May 16 '23 at 13:07

2 Answers2

6

I've just learned that additive operation is faster than multiplicative operation in C

That's nonsense. There is nothing in the C language itself that affects this. Which is faster depends entirely on the instruction set (ISA) provided by the CPU.

Therefore, I'm curious whether (a+b)*c would be calculated faster than a*c+b*c

It is very likely that the optimizing compiler will generate the same machine code no matter which of those versions you write in the C code. Try out this code:

int add1 (int a, int b, int c)
{
  return (a+b)*c;
}

int add2 (int a, int b, int c)
{
  return a*c+b*c;
}

On gcc -O3 13.1 for x86_x64 I get 100% equivalent assembler code for both versions:

add1:
    lea     eax, [rdi+rsi]
    imul    eax, edx
    ret
add2:
    lea     eax, [rsi+rdi]
    imul    eax, edx
    ret
Ian Abbott
  • 15,083
  • 19
  • 33
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • extra kudos for the grumpy. I like grumpy. – Mike Nakis May 16 '23 at 13:02
  • @MikeNakis I bet you just love SO then :) – Lundin May 16 '23 at 13:03
  • Not particularly, because grumpiness is generally frowned upon, (I have tried to be grumpy, my comments get deleted.) But I agree that if it was free for all grumpy then it would not be nearly as useful. So. There's that. – Mike Nakis May 16 '23 at 13:06
  • 2
    @MikeNakis It's actually a big problem that people today read all manner of strange things into one's written text and assume the worst unless you add at least 5 emojis at the end... – Lundin May 16 '23 at 13:14
  • Hi @Lundin Could you please guide me in learning assembly language. I'm very interested but cannot find a proper path and resources – Rituparna Warwatkar May 16 '23 at 13:15
  • @RituparnaWarwatkar Check out the [assembler tag wiki](https://stackoverflow.com/tags/assembly/info) here on SO - lots of good resources there. – Lundin May 16 '23 at 13:17
  • @Lundin Thank you Sir ! Also I had a doubt, I have a laptop that runs on AMD ryzen 5 CPU... If we write programs in assembly language of my CPU, will I be able to run that program on the bare metal without any operating system ? Is that possible ?. Lets say just a simple program that takes input 2 numbers and adds them and displays output. – Rituparna Warwatkar May 16 '23 at 13:44
  • Re "*It is very likely that the optimizing compiler will generate the same machine code*", For integers, quite possibly. But for floats? Compilers don't tend to reorganize float operations because the order is significant because of precision loss. For example, there's a flag for gcc to let it do those rearrangements (whose name I forget). – ikegami May 16 '23 at 13:48
  • @ikegami Float operations should yield a consistent result, so these rearrangements might be not standard compliant. You are probably talking about [`-ffast-math`](https://stackoverflow.com/questions/7420665/what-does-gccs-ffast-math-actually-do) – Eugene Sh. May 16 '23 at 15:09
  • Thanks for the answer!! I was taught that multiplication like`a * b` is actually adding `a` for `b` times so it's slower than additive operation. Is it wrong? – royal grasshopper May 18 '23 at 09:26
0

The only way to know for sure is to measure directly. You can look at the machine code, but that doesn't necessarily tell you how things will be processed; your system may execute operations in parallel.

Write up both versions, instrument the code to gather statistics (either through a profiler or manually), then run the computations several million times.

John Bode
  • 119,563
  • 19
  • 122
  • 198