Which is the fastest?

Started by
8 comments, last by darkforest 17 years, 10 months ago
Is it Test1 or Test2, please explain why... Test1:
int a,b,c;
a=b+c;
Test2:
int a,b,c;
b+=c;
a=b;
(Where it is a empty space there should be a <plus>)
Advertisement
Quote:Original post by darkforest
Is it Test1 or Test2, please explain why...

Test1:
int a,b,c;a=b+c;

Test2:
int a,b,c;b+=c;a=b;


(Where it is a empty space there should be a <plus>)

Does it really matter?
There might be some mundance differences in performance between the two, but will this micro optimiztion will really going to be any benefitial for you?
I would recommend you to choose the one that makes it more east to understand what it does.

It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
Test1, because it has less characters in source code, that is why I can type it faster than Test2.
The compiler will compile these two into the same code, anyways. Something like
mov   eax,    ; eax = badd   eax, [c]   ; eax += cmov   [a], eax   ; a = eax
Since it doesn't have much other options (on the x86, that is).

You should use a = b + c; since that is a bit more readable than b += c; a = b;.

[Edit]: Test2 modifies the variable b, which may not be what you exactly want. You've got the same value in two variables then -- which may be a bit redundant, if you do not need to keep the original b's value.
Since the example is so trivial, any compiler worth being called a compiler with todays standards will produce the same code. Why? Because the end result of the two codes are the same; after the code is executed, a is the sum of b and c, and that is what the compiler is generating code for. Same result, same code.

The only possible exception is the issue Oxyd presented; the fact that you modify b in Test2 could make a difference, but only if you use b later in the program. If you don't, the compiler will ignore the fact that you use b as a temporary storage for the sum.

Todays compilers are not stupid. They will catch something trivial like this and make the best out of it. You write the code in the way you can read it best, and the compiler will make machine code that reproduces the end result. It will not make a one to one mapping between source code statements and machine code.
Why do you care? Use Test1 unless you needs to add a to b anyway. What will happen in the first is something like this (where the first operand is the destination, and we ignore registers since we are just trying to show the instructions):
mov a, badd a, c


While Test2 will be something like this.
add b, cmov b, a


I doubt there will be a big difference, except b is modified in the second.
Quote:Original post by darkforest
Is it Test1 or Test2, please explain why...

Test1:
int a,b,c;a=b+c;

Test2:
int a,b,c;b+=c;a=b;


(Where it is a empty space there should be a <plus>)
Speed is absolutely irrelevant. The two have different behaviours. The second one modifies the value of b whereas the first one does not. Without furthur code to place this in context, the question is meaningless.

Rules of Optimisation:
Rule 1: Don't do it.
Rule 2 (for experts only): Don't do it yet.
- M.A. Jackson
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
As iMalk says, the two have different behaviour, so there's not much point in comparing them. But, to answer your question, Test1 has 1 addition and 1 assignment, whereas Test2 has 1 addition and 2 assignments, so Test1 faster.
I'm pretty sure both tests would put you in the realm of undefined behaviour, I think you have more to worry about than optimization. ;)
Thanks for your answers!

This topic is closed to new replies.

Advertisement