which is faster

L

LuB

I'm sure this has been asked before - but my naive search didn't glean
any results.

Which operation generally involves fewer CPU instructions ... or
specifically, which is generally faster?

RECT rect = { 1, 2, 3, 4 };

or

RECT rect;
rect.left = 1;
rect.top = 2;
rect.right = 3;
rect.bottom = 4;
 
H

Heinz Ozwirk

LuB said:
I'm sure this has been asked before - but my naive search didn't glean
any results.

Which operation generally involves fewer CPU instructions ... or
specifically, which is generally faster?

RECT rect = { 1, 2, 3, 4 };

or

RECT rect;
rect.left = 1;
rect.top = 2;
rect.right = 3;
rect.bottom = 4;

Generally one is faster than the other, but sometimes the other is faster
than the first one, and sometimes you cannot detect any difference. If you
want to know the answer for the compiler, operating system and hardware you
are using, test it! And don't assume that speed increases if the number of
CPU instructions is reduced.

Heinz
 
B

Ben Pope

LuB said:
I'm sure this has been asked before - but my naive search didn't glean
any results.

Which operation generally involves fewer CPU instructions ... or
specifically, which is generally faster?

RECT rect = { 1, 2, 3, 4 };

This is initialisation.
or

RECT rect;

This is default initialisation
rect.left = 1;
rect.top = 2;
rect.right = 3;
rect.bottom = 4;

And assignment.

It's generally preferred to initialise, rather than default initialise
and assign. Whether it's quicker or not is dependant on several things.

Ben Pope
 
R

Rolf Magnus

Heinz said:
Generally one is faster than the other, but sometimes the other is faster
than the first one, and sometimes you cannot detect any difference. If you
want to know the answer for the compiler, operating system and hardware
you are using, test it! And don't assume that speed increases if the
number of CPU instructions is reduced.

In addition, don't assume that the number of CPU instructions is reduced if
you make the source code shorter. Generally, don't assume anything when it
comes to compiler optimization. I've seen compilers do optimizations that
I'd never expect, and I've seen them not do optimizations that I considered
obvious.
 
M

Mike Wahler

Ben Pope said:
This is initialisation.


This is default initialisation

Nit: This is initialization only at file scope.
At block scope, 'rect' is not initialized at all.

-Mike
 
R

Ron Natalie

Mike said:
Nit: This is initialization only at file scope.
At block scope, 'rect' is not initialized at all.

Depends on the type of RECT. You can have non-POD aggregates.
 
M

Mike Wahler

Ron Natalie said:
Depends on the type of RECT. You can have non-POD aggregates.

Right. I was assuming that the 'RECT' type is
the one from the Windows API. But we all know
what happens when we assume ... :)

-Mike
 
B

Ben Pope

Mike said:
Right. I was assuming that the 'RECT' type is
the one from the Windows API. But we all know
what happens when we assume ... :)

It makes an "ass" out of "u" and "me".

Ben Pope
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top