tags:

views:

144

answers:

5

When declaring primitives does it matter if I use the assignment operator or copy constructor? Which one gives the optimal performance, or do most compilers today compile these two statements to the same machine code?

int i = 1;
int j( 1 );
+3  A: 

I'm fairly certain that they are the same, however if you do a static initialization, there is no initialization during runtime, the value is built into the binary.

static int i = 1;

Though, this isn't always appropriate, given the meaning of the keyword static.

Sufian
Given that they're effectively the same in performance, I recommend using the assignment operator. It's more clear what you're doing.
Mark
More clear is relative, some people could argue that constructor like syntax is more clear as it looks more OO. (I prefer assignment).
Martin York
+4  A: 

Same for:

  int i = 1;
  int j( 1 );

Not for:

  Cat d;
  Cat c1;
  c1 = d;
  Cat c2(d);//<-- more efficient
Brian R. Bondy
Why is it more efficient? The default constructor is called for c1 first?
Bastien Léonard
They have the same type. It doesn't matter here. Cat c1 = d; and Cat c2(d); is going to do the same. It matters if they have different type and not derived, then the = d; case will do Cat c1((Cat(d))); effectively, but the (d) case is doing C c2(d); (oh what surprise! :))
Johannes Schaub - litb
Are you sure? What's the difference between "Cat c1 = d;" and "Cat c2(d);"? I thought they would both invoke the same copy constructor (that even though the first one looks like an assignment, it's a copy).
ChrisW
Note that GCC throws an error even on that case if the copy constructor is explicit. But comeau doesn't do so. I tend to think GCC is right here, though. But w.r.t performance, they are both the same
Johannes Schaub - litb
I see, good point about the different types
Brian R. Bondy
A a = b; is copy init, and A a(b); is direct init, and then the description of behavior starts with "— If the initialization is direct-initialization, or if it is copy-initialization where the cv-unqualified version of the source type is the same class as, or a derived class of, the class of the destination, constructors are considered. ...". I too find this confusing, though :)
Johannes Schaub - litb
+2  A: 

Me think both will be compiled the same; just use the one you feel is the most readable for you and other who might read your code.

Max
A: 

In general, this question can't be answered. For primitive types a good compiler will reduce them to the same Assembly instructions, but for user-defined types (which is to say, classes) you're at the mercy of whoever wrote the copy and assignment operators. I've seen code where one was faster than the other by an order of magnitude, because the person who wrote the copy constructor wasn't the same as the person who wrote the assignment operator -- and one of them did it smart and one of them didn't.

Except that the question IS specifically about primitive types.
sean e
A: 

In the case of a single primitive type it's not going to matter.

When it can matter is when you have an aggregate of primitive types. In other words, a POD. In that case, there is no more efficient way to instantiate it that by using the aggregate initializer syntax:

struct POD
{  
  int n;
  double d;
  bool b;
};

If that's your POD, then this:

POD pod = {1, 3.14, true};

...can't be beat in terms of efficiency.

John Dibling