| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"" |
| Date: |
28 Jan 2008 05:51:24 AM |
| Object: |
Another quest for speed |
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?
.
|
|
| User: "Alf P. Steinbach" |
|
| Title: Re: Another quest for speed |
28 Jan 2008 06:01:50 AM |
|
|
* michael.goossens@gmail.com:
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
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.
|
|
|
|
| User: "Jensen Somers" |
|
| Title: Re: Another quest for speed |
28 Jan 2008 06:07:03 AM |
|
|
wrote:
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
.
|
|
|
|
| User: "Phil Endecott" |
|
| Title: Re: Another quest for speed |
28 Jan 2008 11:15:29 AM |
|
|
wrote:
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.
.
|
|
|
|

|
Related Articles |
|
|