9
9lives.9lives
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:perator[](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;
};
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
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:perator[](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;
};
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