473,791 Members | 3,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Optimization: const int& ?

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::opera tor[](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

Oct 7 '07 #1
4 4726
On 2007-10-07 01:51:28 -0400, "9lives.9li ves" <ba*******@hotm ail.comsaid:
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::opera tor[](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

Oct 7 '07 #2
9lives.9lives wrote:
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::opera tor[](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.
Oct 7 '07 #3
9lives.9lives wrote:
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::opera tor[](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;
}
Oct 7 '07 #4

"9lives.9li ves" <ba*******@hotm ail.comwrote in message news:11******** **************@ 22g2000hsm.goog legroups.com...
[snip]
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

Oct 9 '07 #5

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

Similar topics

2
16247
by: CoolPint | last post by:
Can anyone clearly explain the difference between constant reference to pointers and reference to constant pointers? What is const int * & ? Is it a constant reference to a pointer to an integer? Or Is it a reference to a pointer to a constant integer? What is being constant in this case? The pointer or the integer being pointed? How about int * const & ? Is this a reference to a constant pointer to an integer? Or
4
3913
by: Marcin Vorbrodt | last post by:
I understand that compiler may optimize better, if as parameters to my functions i pass: const Object &o instead of Object &o All the implicit type conversion and all sounds good (Effective C++ rules).
25
2946
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default construction and then.. // some other processing and/or changing 'retval' return retval; }
6
15365
by: p|OtrEk | last post by:
What is practic difference between this two declarations? If i want call my func with func("blah") i could write: 1) func(std::string const& arg1) 2) func(const std::string& arg1) Whats better to use if i dont want to change content of arg1 it in func body? -- << pozdrawiam -lysek- @ irc.freenode.net#linux.com.pl << prompt$ :(){ :|:& };: << echo mail | sed 's/__NOSPAM//g'
0
1711
by: tom olson | last post by:
After more searching I found that defining const operators can cause problems with many compilers due to the way it interprets the C++ standard. I removed the const operators from my class and it seems to be working fine now. >-----Original Message----- >I have C++ code that has been running just fine in VS6 for >a couple of years. I brought it into VS.NET with managed
9
1858
by: miaohua1982 | last post by:
the program is as follows: #include <vector> using namespace std; class A{}; int main() { A* const &p = NULL; vector<A*B(3,NULL); //there is a compile error B.push_back(NULL);
13
3985
by: dragoncoder | last post by:
Hi everyone, please consider the following function:- const int& foo ( const double& d ) { return d; } g++ compiles it with warnings and solaris CC gives error. I want to know if the code is correct according to the standard ?
2
2217
by: ek | last post by:
This first example does not work (cannot be overloaded): int& operator()(int a) { // (1) return a; } int const& operator()(int a) { // (2) return a; }
2
9155
by: nassim.bouayad.agha | last post by:
Hello, here is a code snippet showning my problem : template<typename _K> class TClass1 { public: void Process(const _K& arg) const {
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9512
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10201
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10147
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9987
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9023
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7531
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4100
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3709
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.