473,385 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Optimization by hand.

In the following code I have attempted to perform all calculations that are
repeated in the mathematical expression for the rotation being evaluated.
IOW, I calculate cos(_angle) only once, and put it in a variable. Perhaps
a compiler is smart enough to do this for me. I don't know. When I get a
chance I plan on trying to look at the compiler output to determine what is
happening with gcc.

I'm wondering what others think about this kind of hand optimization.

Matrix3<T> setRotation(const Vector3<T>& axis, const T& angle)
{
axis.normal(_axis); // puts unit-length vector parallel to axis in _axis
_angle = angle;

// need to specialize these for float
T c = cos(_angle);
T s = sin(_angle);
T t = T(1) - c;

// references should vanish at compile time
const T& x = axis[0];
const T& y = axis[1];
const T& z = axis[2];

T txy = t * x * y;
T& tyx(txy);

T txz = t * x * z;
T& tzx(txz);

T tyz = t * y * z;
T& tzy(tyz);

T sx = s * x;
T sy = s * y;
T sz = s * z;

// The following is based on the articel found here
// http://www.gamedev.net/reference/art...rticle1199.asp
// I have not yet tested the operations to verify my correction to
// the formula in the article are valid.
_data[0][0] = t*x*x+c; _data[0][1] = tyx + sz; _data[0][2] = tzx-sy;
_data[1][0] = txy-sz; _data[1][1] = t*y*y+c; _data[1][2] = tzy+sx;
_data[2][0] = txz+sy; _data[2][1] = tyz-sx; _data[2][2] = tzz+c;

/* my original form
_data[0][0]=t*x*x+c; _data[0][1]=t*y*x+s*z; _data[0][2]=t*z*x-s*y;
_data[1][0]=t*x*y-s*z; _data[1][1]=t*y*y+c; _data[1][2]=t*z*y+s*x;
_data[2][0]=t*x*z+s*y; _data[2][1]=t*y*z-s*x; _data[2][2]=t*z*z+c;
*/

}

/************************************************** *************************
*****Copyright*(C)*2004*by*Steven*T.*Hatton******* **************************
*****ha*****@globalsymmetry.com******************* **************************
************************************************** **************************
*****This*program*is*free*software;*you*can*redist ribute*it*and/or*modify***
*****it*under*the*terms*of*the*GNU*General*Public* License*as*published*by***
*****the*Free*Software*Foundation;*either*version* 2*of*the*License,*or******
*****(at*your*option)*any*later*version.********** **************************
************************************************** **************************
*****This*program*is*distributed*in*the*hope*that* it*will*be*useful,********
*****but*WITHOUT*ANY*WARRANTY;*without*even*the*im plied*warranty*of*********
*****MERCHANTABILITY*or*FITNESS*FOR*A*PARTICULAR*P URPOSE.**See*the**********
*****GNU*General*Public*License*for*more*details.* **************************
************************************************** **************************
*****You*should*have*received*a*copy*of*the*GNU*Ge neral*Public*License******
*****along*with*this*program;*if*not,*write*to*the **************************
*****Free*Software*Foundation,*Inc.,************** **************************
*****59*Temple*Place*-*Suite*330,*Boston,*MA**02111-1307,*USA.**************
* Gunax lbh sbe gur Qbanyq Xahgu dhbgr. Yvxrjvfr ba vg'f vzcyrzragngvba *
* qrcraqrag. *
************************************************** **************************/
--
Gunax lbh sbe gur Qbanyq Xahgu dhbgr.
Yvxrjvfr ba vg'f vzcyrzragngvba qrcraqrag.
Jul 22 '05 #1
5 1398

"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message
news:Q_********************@speakeasy.net...
In the following code I have attempted to perform all calculations that are repeated in the mathematical expression for the rotation being evaluated.
IOW, I calculate cos(_angle) only once, and put it in a variable. Perhaps
a compiler is smart enough to do this for me. I don't know. When I get a
chance I plan on trying to look at the compiler output to determine what is happening with gcc.

I'm wondering what others think about this kind of hand optimization.


I think one should give the compiler first crack at it,
then only 'hand optimize' if necessary. But even before
doing that, I'd first try a different compiler(s).

-Mike
Jul 22 '05 #2
Mike Wahler wrote:

"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message
news:Q_********************@speakeasy.net...
In the following code I have attempted to perform all calculations that

are
repeated in the mathematical expression for the rotation being evaluated.
IOW, I calculate cos(_angle) only once, and put it in a variable.
Perhaps
a compiler is smart enough to do this for me. I don't know. When I get a
chance I plan on trying to look at the compiler output to determine what

is
happening with gcc.

I'm wondering what others think about this kind of hand optimization.


I think one should give the compiler first crack at it,
then only 'hand optimize' if necessary. But even before
doing that, I'd first try a different compiler(s).

-Mike

The real question is whether I have removed operations from the object code,
and/or replaced them with more efficient operations. Sure, I can and will
experiment with this. I know from experience that when I start performing
multiple nested operations on nodes in a tree the cost can get very high in
a hurry. O(log(n)) IIRC.

I'm not trying to solve one problem. I'm trying to address an entire class
of problems. If there are formal discussions of these issues available in
articels or books, I am interested to know where.
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

Jul 22 '05 #3


"Steven T. Hatton" wrote:
Mike Wahler wrote:

"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message
news:Q_********************@speakeasy.net...
In the following code I have attempted to perform all calculations that

are
repeated in the mathematical expression for the rotation being evaluated.
IOW, I calculate cos(_angle) only once, and put it in a variable.
Perhaps
a compiler is smart enough to do this for me. I don't know. When I get a
chance I plan on trying to look at the compiler output to determine what

is
happening with gcc.

I'm wondering what others think about this kind of hand optimization.


I think one should give the compiler first crack at it,
then only 'hand optimize' if necessary. But even before
doing that, I'd first try a different compiler(s).

-Mike

The real question is whether I have removed operations from the object code,
and/or replaced them with more efficient operations. Sure, I can and will
experiment with this. I know from experience that when I start performing
multiple nested operations on nodes in a tree the cost can get very high in
a hurry. O(log(n)) IIRC.

I'm not trying to solve one problem. I'm trying to address an entire class
of problems. If there are formal discussions of these issues available in
articels or books, I am interested to know where.
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell


the problem is dependent both on the processor you are using and the compiler
you are using. Most modern compilers will be able to cache often used integer
values in registers, and on some processors this will happen for often used
floating values as well. But it is really up to the compilers judgement as to
what constitutes 'often used' and how much trouble the compiler author went to.
Most modern day compilers will do a good job of this if the processor lets them
(for example on intel processors it would be somewhat difficult but not
impossible to cache floating values because of the floating point stack
architecture, but depending on usage and scope integers are easily cacheable).
But there was a time in the not too distant past where compilers were *so*
dismal that if you wanted this kind of optimization you *had* to do it by hand
the way you are doing it here.

David
Jul 22 '05 #4
"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message
news:vt********************@speakeasy.net...
Mike Wahler wrote:

"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message
news:Q_********************@speakeasy.net...
In the following code I have attempted to perform all calculations that are
repeated in the mathematical expression for the rotation being evaluated. IOW, I calculate cos(_angle) only once, and put it in a variable.
Perhaps
a compiler is smart enough to do this for me. I don't know. When I get a chance I plan on trying to look at the compiler output to determine
what is
happening with gcc.

I'm wondering what others think about this kind of hand optimization.


I think one should give the compiler first crack at it,
then only 'hand optimize' if necessary. But even before
doing that, I'd first try a different compiler(s).

-Mike

The real question is whether I have removed operations from the object

code, and/or replaced them with more efficient operations.
That depends entirely upon the implementation (and upon the
platform where the program is executed). The question cannot
be answered from a language perspective.
Sure, I can and will
experiment with this. I know from experience that when I start performing
multiple nested operations on nodes in a tree the cost can get very high in a hurry. O(log(n)) IIRC.
Yes it can, at the algorithmic level. How various compiler implement
the algorithm can vary widely, resulting in widely differing performance.
I'm not trying to solve one problem. I'm trying to address an entire class of problems. If there are formal discussions of these issues available in
articels or books, I am interested to know where.


For discussion of algorithmic solutions, try an algorithms group.
And there are also always the famous Knuth books.

For discussion of the efficiency of implementation of particular
compilers, ask in groups about those compilers.

I can't at the moment think of any particular books or articles
about these issues, but google's always a good place to start. :-)

-Mike
Jul 22 '05 #5
"Mike Wahler" <mk******@mkwahler.net> wrote...
[...]
I can't at the moment think of any particular books or articles
about these issues, but google's always a good place to start. :-)


Efficient C++, maybe...
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: josef angermeier | last post by:
i wrote a display driver for a lcd segment display which doesnt recognize ascii character so that each character of an output string needs to be converted byte by byte by looking in a const...
22
by: NigelW | last post by:
This is really a question for the development team. Are there plans to improve the optimization of C# to MSIL? I ask this, as inspection with ILDASM of the MSIL code shows that, even with the...
3
by: Amol | last post by:
I am working on an interesting graph optimization problem and I would like to have a few expert opinions for helping me with a solution. So here goes ... I have a black box with a complex...
5
by: wkaras | last post by:
I've compiled this code: const int x0 = 10; const int x1 = 20; const int x2 = 30; int x = { x2, x0, x1 }; struct Y {
10
by: SzH | last post by:
The code below demonstrates that the copy constructor of moo is not called on the first line of main. In spite of this, g++ (version 4.1.2) refuses to compile it if I make the copy constructor...
206
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a...
10
by: Markus Grunwald | last post by:
Hello, while implementing a simple algorithm for digital filters (IIR filters), I got very confused about the very bad performance. This is the code, discussion follows: /**...
1
by: Rahul | last post by:
While reading "Efficient C++" by Dov Bulka I came across the follown statement "In addition, you must also define a copy constructor to "turn on" the Return Value Optimization(RVO). If the class...
20
by: Ravikiran | last post by:
Hi Friends, I wanted know about whatt is ment by zero optimization and sign optimization and its differences.... Thank you...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.