Connecting Tech Pros Worldwide Help | Site Map

Optimization: const int& ?

9lives.9lives
Guest
 
Posts: n/a
#1: Oct 7 '07
Hello, everyone! I am trying to optimize some code, but I don't think
I'm doing what I think I'm doing. I profiled my code and found that
the overloaded [] operator of my monomial class did
6384690328 calls in 33.67 seconds.

33.67 6384690328 Monomial::operator[](int) const

Since the parameter was an int, I made it const int& since I reasoned
I would be saving a copy of the int parameter to the stack by changing
from a pass-by-value to pass-by-reference. However, when I next ran
the code the overloaded operator made
6384690328 calls in 71.86 seconds.

Changing the from int to const int& actually slowed it down, more than
doubled the speed! I wonder if anyone can tell me why? My reasoning
must be faulty...

Here is the full function declaration:
inline int operator[](const int& i) const
{
assert(0 <= i && i < n);
return exp[i];
};


exp is an int*.

Any help or comments on how to speed up this simple function would be
most, most appreciated!

Very best regards,
Susan

Kira Yamato
Guest
 
Posts: n/a
#2: Oct 7 '07

re: Optimization: const int& ?


On 2007-10-07 01:51:28 -0400, "9lives.9lives" <bajichuan@hotmail.comsaid:
Quote:
Hello, everyone! I am trying to optimize some code, but I don't think
I'm doing what I think I'm doing. I profiled my code and found that
the overloaded [] operator of my monomial class did
6384690328 calls in 33.67 seconds.
>
33.67 6384690328 Monomial::operator[](int) const
>
Since the parameter was an int, I made it const int& since I reasoned
I would be saving a copy of the int parameter to the stack by changing
from a pass-by-value to pass-by-reference. However, when I next ran
the code the overloaded operator made
6384690328 calls in 71.86 seconds.
>
Changing the from int to const int& actually slowed it down, more than
doubled the speed! I wonder if anyone can tell me why? My reasoning
must be faulty...
>
Here is the full function declaration:
inline int operator[](const int& i) const
{
assert(0 <= i && i < n);
return exp[i];
};
>
>
exp is an int*.
>
Any help or comments on how to speed up this simple function would be
most, most appreciated!
>
Very best regards,
Susan
The integer probably has the same size as a pointer. So you're not
saving much passing by value vs. passing by reference in this case.

However, when you pass by reference, you're really passing a pointer.
So, when the operator[] function uses it, it has to dereference it to
get the actual value of the integer. So, you're actually increasing
work at runtime.

Just a guess.

--

-kira

Ian Collins
Guest
 
Posts: n/a
#3: Oct 7 '07

re: Optimization: const int& ?


9lives.9lives wrote:
Quote:
Hello, everyone! I am trying to optimize some code, but I don't think
I'm doing what I think I'm doing. I profiled my code and found that
the overloaded [] operator of my monomial class did
6384690328 calls in 33.67 seconds.
>
33.67 6384690328 Monomial::operator[](int) const
>
Since the parameter was an int, I made it const int& since I reasoned
I would be saving a copy of the int parameter to the stack by changing
from a pass-by-value to pass-by-reference. However, when I next ran
the code the overloaded operator made
6384690328 calls in 71.86 seconds.
>
Changing the from int to const int& actually slowed it down, more than
doubled the speed! I wonder if anyone can tell me why? My reasoning
must be faulty...
>
Implementation specific behaviour, but when passing by reference the
code has to take the address of the variable passed. The variable may
well have been in a register which would simply be pushed on the stack.
One some machines, the compiler could place the variable in a register
which can be accessed by the called function without using the stack.

--
Ian Collins.
Gianni Mariani
Guest
 
Posts: n/a
#4: Oct 7 '07

re: Optimization: const int& ?


9lives.9lives wrote:
Quote:
Hello, everyone! I am trying to optimize some code, but I don't think
I'm doing what I think I'm doing. I profiled my code and found that
the overloaded [] operator of my monomial class did
6384690328 calls in 33.67 seconds.
>
33.67 6384690328 Monomial::operator[](int) const
>
Since the parameter was an int, I made it const int& since I reasoned
I would be saving a copy of the int parameter to the stack by changing
from a pass-by-value to pass-by-reference. However, when I next ran
the code the overloaded operator made
6384690328 calls in 71.86 seconds.
>
Changing the from int to const int& actually slowed it down, more than
doubled the speed! I wonder if anyone can tell me why? My reasoning
must be faulty...
>
Here is the full function declaration:
inline int operator[](const int& i) const
{
assert(0 <= i && i < n);
return exp[i];
};
I'd expect that the compiler's optimizer would produce identical inlined
code. Perhaps it's an optimizer bug ? Do you know what optimization
flags you turned on in your compiler ?

I just tried it with this code below and gcc 3.4.4 and 4.1.1 and it
produces virtually identical code for the tot function.

#include <cassert>

#ifndef REF
typedef int T;
#else
typedef int & T;
#endif

struct X
{
int * exp;
int n;

inline int operator[](const T i) const
{
assert(0 <= i && i < n);
return exp[i];
};
};


int tot( const X & x )
{

register int totval = 0;

for ( register int i = 0; i < x.n; ++i )
{
totval += x[i];
}
return totval;
}
Alex Vinokur
Guest
 
Posts: n/a
#5: Oct 9 '07

re: Optimization: const int& ?



"9lives.9lives" <bajichuan@hotmail.comwrote in message news:1191736288.903725.232820@22g2000hsm.googlegro ups.com...
[snip]
Quote:
Any help or comments on how to speed up this simple function would be
most, most appreciated!
[snip]

This might be of interest to you.

http://groups.google.com/group/comp....6f823e49e07e93

http://groups.google.com/group/comp....97a8b1c24bca3b


--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn



Closed Thread