3

I have been trying out C++ and I am particularly interested in the performance of two scripts. A small intro:

I have a class called Point for experimenting with points given in polar coordinates. The class contains two private double variables, the usual functions get, set and the public function rotate which takes a double argument and adds it to our current angle in the polar form to produce a new Point object.

Below follow two different scripts for the function rotate:

void Point::rotate(double theta) {
    double A = getA();
    A += theta;
    setA(A);
}

void Point::rotate(double theta) {
    setA(getA() + theta);
} 

My question is straightforward:

Which one is practically faster and why ?

I understand that the first method has to use getA() and then save that into the variable A so most likely, it takes longer/is less efficient. In more generality, upon calculating an expression, is there ever a need to save big parts of the expression in other variables and then use these ? (With the exaggerated assumption that the "person" who wrote the code will not make a mistake and everyone who might have to read the code later will perfectly understand it.)

A simple example to clarify my questions:

Say we wanted to calculate a+b+c. Is it better to save a+b in a new variable, say d, and then add d to c ? How about calling a function with argument another function evaluation ?

Thanks in advance!

Alex Apas
  • 65
  • 4

1 Answers1

7

Both of these expressions are identical. Ideally you could always run a benchmark in which you call the expression multiple times in a loop and see the time difference.

However, another way of looking at it is by answering the second part of question, which speaks about a+b+c. When the code will be translated to assembly, a+b will anyway be stored in some register and then added to c, as no operation for 3 figure addition is present in assembly. So, there won't be a difference in:

c =  a + b + c

and

d = a + b
c = c + d

Also, many other optimisations are done by compilers, which result in such things not making a difference.

therainmaker
  • 4,253
  • 1
  • 22
  • 41
  • Is it not that in the first case c (one variable) is put on the stack and in the second case two variables are put on the stack? Resulting in one more push/pop? – fonZ Jul 15 '15 at 10:45
  • @fonZ: My point is that when converted to assembly, even `c = a+b+c` is two operations, which is identical to the second part. There will be an intermediate variable in the calculation of `a+b+c` but that is hidden from you. – therainmaker Jul 15 '15 at 10:47
  • 3
    @fonZ Typically not in a production compiler. In a toy compiler or something really unoptimized like Tiny C Compiler, that might be the case. In compilers like MSVC, GCC, ICC, Clang, typically no. Those compilers are going to chew through such expressions and effectively treat both as the same thing, and given so few live variables, typically with no stack spills whatsoever. A quick glance at disassembly should show this. –  Jul 15 '15 at 10:48
  • 2
    @fonZ Compilers pretty reliably remove unnecessary push/pop operations on the stack. In the end not the program you wrote is executed, but a different program that is much faster but has the same observable result. That even happens if you write assembly nowadays. – nwp Jul 15 '15 at 10:48
  • Heck, modern compilers will look at `a+b+c` and go check what's already in a register. If b and c are already in a register but a needs to be loaded, they'll overlap the load of a and the addition of b+c so that there's onl one addition left when the load completes. That means you cannot speak about the speed of any isolated bit of code; the context in which it's used determines the binary code generated. – MSalters Jul 15 '15 at 10:56
  • This is exactly what I was looking for! Thanks again. – Alex Apas Jul 15 '15 at 13:15