help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

C++ optimization help with the creation of temporaries


From: edA-qa mort-ora-y
Subject: C++ optimization help with the creation of temporaries
Date: Thu, 24 Mar 2005 21:51:03 +0100
User-agent: Mozilla Thunderbird 0.8 (X11/20040913)

I have a situation where it appears some of the gcc optimizations are not taking effect, or they are performing very poorly, and I need help in figuring out how to improve the code.

The basic test setup is a timing of the following two loops (functions explained further, but it works like a point would be expected to):

//setup vars
const pointx pa( 1, 2, 3 );
const pointx pb( 4, 5, 6 );
const rtype ca = 0.17;
const rtype cb = 57.3;
const int loop = 2000000;

//Fast loop (0.07s)
    pointx target;
    for( int i=0; i < loop; i++ )
        for( unsigned j=0; j < 3; j++ )
        {
            target[j] += pa[j];
            target[j] *= ca;
            target[j] -= pb[j] * cb;
        }

//Slow loop (0.23s)
    pointx target;
    for( int i=0; i < loop; i++ )
    {
        target += pa;
        target *= ca;
        target -= pb * cb;    //most time here
    }

In the second last line of the second item I note most of the time is spent there, so I'm guessing there is a temporary created. The multiply function for a point and scalar is:

    //dim is template parameter for point size
    point operator*(const rtype m ) const
    {
        point n( *this );
        for( unsigned i=0; i < dim; i++ )
            n.vals[i] *= m;
        return n;
    }

I've tried optimizing this function in every fashion, but every change just makes it slower:
    -return &n
    -n.vals[i] = vals[i] * m, also with
        -point n;    //not initializor, take advantage of NRVO
        -point n( uninit );    //special ctro for no initialization

Note: pointx is simply type point<3>

The speed difference is rather extreme, and as this type of calculation is used throughout my code it can be attributed to a major slowdown in my codebase.

Any help would be appreciated.

(Compiling using GCC 3.3.1)

//extra notes
//Copy Ctor
    point( const point& o )
    {
        for( unsigned i=0; i < dim; i++ )
            vals[i] = o.vals[i];
    }

--
edA-qa mort-ora-y
Idea Architect
http://disemia.com/

--
edA-qa mort-ora-y
Idea Architect
http://disemia.com/


reply via email to

[Prev in Thread] Current Thread [Next in Thread]