Another quest for speed

M

michael.goossens

BBox::Expand(float delta){
Vector d = Vector(delta, delta, delta);
p_min -= d;
p_max += d;
}

BBox::Expand(float delta){
p_min -= Vector(delta, delta, delta);
p_max += Vector(delta, delta, delta);
}

Logically the first method would only make 1 Vector object and the
second 2, so method two would take more cpu instructions? Or does c++
do some intern stuff to optimise this?
 
A

Alf P. Steinbach

* (e-mail address removed):
BBox::Expand(float delta){
Vector d = Vector(delta, delta, delta);
p_min -= d;
p_max += d;
}

BBox::Expand(float delta){
p_min -= Vector(delta, delta, delta);
p_max += Vector(delta, delta, delta);
}

Logically the first method would only make 1 Vector object and the
second 2, so method two would take more cpu instructions? Or does c++
do some intern stuff to optimise this?

Depends on your compiler and compiler settings.

Measure.

If it matters.


Cheers, & hth.,

- Alf
 
J

Jensen Somers

BBox::Expand(float delta){
Vector d = Vector(delta, delta, delta);
p_min -= d;
p_max += d;
}

BBox::Expand(float delta){
p_min -= Vector(delta, delta, delta);
p_max += Vector(delta, delta, delta);
}

Logically the first method would only make 1 Vector object and the
second 2, so method two would take more cpu instructions? Or does c++
do some intern stuff to optimise this?

Any C++ compiler will always try to optimize things as best as it can.
Check the documentation of your compiler to see what and how it
optimizes different things.

- Jensen
 
P

Phil Endecott

BBox::Expand(float delta){
Vector d = Vector(delta, delta, delta);
p_min -= d;
p_max += d;
}

BBox::Expand(float delta){
p_min -= Vector(delta, delta, delta);
p_max += Vector(delta, delta, delta);
}

Logically the first method would only make 1 Vector object and the
second 2, so method two would take more cpu instructions? Or does c++
do some intern stuff to optimise this?

It's really easy to measure this sort of thing: write the code (looks
like you've already done it), add a simple main() that calls it a few
zillion times, compile with all available optimisations enabled, and
measure execution time. That will give you a more accurate answer for
your compiler and hardware than all the people here can offer, and it's
quicker.

FWIW, my guess is that as long as the caller can see Vector's
constructor and BBox::Expand and the rest inline, then you'll get
essentially optimal code from both.

If you're coding for an x86 system and performance is vital, then you
may like to investigate using SIMD instructions for this sort of thing,
i.e. processing the x, y and z components in parallel. How to do that
is off-topic for this group, but it's likely to make more impact than
tweaking the details of the C++ coding style.


Phil.
 

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

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,518
Latest member
RomanGratt

Latest Threads

Top