 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Charles Herman Guest
|
Posted: Sat Mar 06, 2004 12:02 am Post subject: why are classes so slow? |
|
|
I have the following two programs:
PROG I
void main()
{
int *v = new int[NUM];
for (int i = 0; i < NUM; ++i)
v[i] = i;
}
PROG II
class rvector
{
private:
int* vals;
size_t vsize;
public:
explicit rvector( size_t vSize ) { vals = new int[vsize]; }
~rvector() { delete [] vals; }
int& operator[] (size_t idx ) { return vals[idx]; }
};
void main()
{
rvector v(NUM);
for (i = 0; i < NUM; ++i)
v[i] = i;
}
I left out some non-germane lines, eg, defining NUM and including header
files.
I compiled and ran these programs using VC++ 6.0. The second program
required approx 6 times as much time to run as the first. What is happening
in program II that takes so much time?
-charles
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dhruv Matani Guest
|
Posted: Sat Mar 06, 2004 10:08 am Post subject: Re: why are classes so slow? |
|
|
On Fri, 05 Mar 2004 19:02:53 -0500, Charles Herman wrote:
| Quote: | I have the following two programs:
PROG I
void main()
{
int *v = new int[NUM];
for (int i = 0; i < NUM; ++i)
v[i] = i;
}
PROG II
class rvector
{
private:
int* vals;
size_t vsize;
public:
explicit rvector( size_t vSize ) { vals = new int[vsize]; }
~rvector() { delete [] vals; }
int& operator[] (size_t idx ) { return vals[idx]; }
};
void main()
{
rvector v(NUM);
for (i = 0; i < NUM; ++i)
v[i] = i;
}
I left out some non-germane lines, eg, defining NUM and including header
files.
I compiled and ran these programs using VC++ 6.0. The second program
required approx 6 times as much time to run as the first. What is happening
in program II that takes so much time?
|
Just one of the many reasons you should be using classes :-)
No, seriously, the first program is a massive memory leak, because you
never delete[] the newed[] array! Whereas the 2nd one does that for you
even without you writing anything to that effect in the main function.
All the more reason to use classes.
Probably there lies the problem? Also, are you compiling in debug mode or
release mode for VC?
--
Regards,
-Dhruv.
Proud to be a Vegetarian.
http://www.vegetarianstarterkit.com/
http://www.vegkids.com/vegkids/index.html
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
David Baraff Guest
|
Posted: Sat Mar 06, 2004 10:09 am Post subject: Re: why are classes so slow? |
|
|
"Charles Herman" <cherman (AT) no (DOT) spam> wrote
| Quote: | I have the following two programs:
PROG I
void main()
{
int *v = new int[NUM];
for (int i = 0; i < NUM; ++i)
v[i] = i;
}
|
Try:
int sum = 0;
int *v = new int[NUM];
for (int i = 0; i < NUM; ++i) {
v[i] = i;
sum += v[i];
}
printf("sum is %dn", sum);
and equivalently in your revector version.
I have seen optimizers smart enough to realize that your loop in the
first case can be skipped completely. Also, unless NUM is absolutely
huge, you're going to be hard put to get a meaningful time measure on
this. NUM would have to at a minimum be several million before you
could even record the amount of time needed by your program.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carl Daniel Guest
|
Posted: Sat Mar 06, 2004 10:10 am Post subject: Re: why are classes so slow? |
|
|
Charles Herman wrote:
| Quote: | I compiled and ran these programs using VC++ 6.0. The second program
required approx 6 times as much time to run as the first. What is
happening in program II that takes so much time?
|
You must've timed an unoptimized build. When I do the same test (your code,
with the typos fixed), I get (NUM=10000000) 0.069365 seconds for Prog1 and
0.069571 seconds for Prog2.
-cd
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Sat Mar 06, 2004 3:13 pm Post subject: Re: why are classes so slow? |
|
|
"Charles Herman" <cherman (AT) no (DOT) spam> writes:
| Quote: | I have the following two programs:
PROG I
void main()
|
Should be int, but who cares?
| Quote: | {
int *v = new int[NUM];
for (int i = 0; i < NUM; ++i)
v[i] = i;
}
PROG II
class rvector
{
private:
int* vals;
size_t vsize;
public:
explicit rvector( size_t vSize ) { vals = new int[vsize]; }
~rvector() { delete [] vals; }
int& operator[] (size_t idx ) { return vals[idx]; }
};
void main()
{
rvector v(NUM);
for (i = 0; i < NUM; ++i)
v[i] = i;
}
I left out some non-germane lines, eg, defining NUM and including
header files.
I compiled and ran these programs using VC++ 6.0. The second
program required approx 6 times as much time to run as the first.
What is happening in program II that takes so much time?
|
The operator[] is a non-inline function, not a built-in operator.
(There are other things as well, but if NUM is reasonably large, we can
ignore them.)
In the first case, the compiler will generate code to add the scaled
index to the pointer inline, then dereference the pointer -- if
optimization is enabled, it will probably just increment the pointer
each time through the loop, and all you have is a simple dereference.
In the second case, you call a function. With VC++, on Intel, that
means that you push the index on the stack, push the address of the
class on the stack, call the function, set up the function stack frame,
read the address of the class from the stack, read the address of the
array from that, read the index from the stack, scale it and add it to
the pointer, before returning the pointer (which you then dereference as
before).
This is what is refered to as the abstraction penalty. C++ was designed
to make it as low as possible. Providing the compilers collaborate, and
providing you make an effort as well. If you really do have performance
problem with your program, try making the operator[] inline. And turn
optimization on. With an inline operator[] and halfway decent
optimization, the code generated for the loop should then be exactly the
same for both programs.
(Generally, I avoid inline until I know that I've got a performance
problem. An operator[] for a vector like class is very definitly an
exception, however, and I probably would have made it inline from the
start.)
--
James Kanze mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France +33 1 41 89 80 93
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Sat Mar 06, 2004 3:16 pm Post subject: Re: why are classes so slow? |
|
|
"Carl Daniel" <cpdaniel_remove_this_and_nospam (AT) mvps (DOT) org.nospam> writes:
| Quote: | Charles Herman wrote:
I compiled and ran these programs using VC++ 6.0. The second
program required approx 6 times as much time to run as the first.
What is happening in program II that takes so much time?
You must've timed an unoptimized build. When I do the same test
(your code, with the typos fixed), I get (NUM=10000000) 0.069365
seconds for Prog1 and 0.069571 seconds for Prog2.
|
With VC++ (on an Intel, presumably)? Unless the compiler is actually
inlining operator[] (which wouldn't be really surprising, given that it
is in the same module), the cost of a function call has to be more, by a
significant amoung, than the cost of the assignment afterwards.
On a more modern processor, a function call might be a lot cheaper
(because parameters are probably passsed in registers, rather than on
the stack), but it is still going to represent a significant amount of
time compared to what he is doing with the results.
--
James Kanze mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France +33 1 41 89 80 93
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carl Daniel Guest
|
Posted: Sun Mar 07, 2004 1:49 am Post subject: Re: why are classes so slow? |
|
|
James Kanze wrote:
| Quote: | The operator[] is a non-inline function, not a built-in operator.
(There are other things as well, but if NUM is reasonably large, we
can ignore them.)
|
Surely operator [] is already inline - it's defined within the body of the
class definition and is therefore inline by default(9.3/2).
-cd
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carl Daniel Guest
|
Posted: Sun Mar 07, 2004 1:50 am Post subject: Re: why are classes so slow? |
|
|
James Kanze wrote:
| Quote: | With VC++ (on an Intel, presumably)? Unless the compiler is actually
inlining operator[] (which wouldn't be really surprising, given that
it is in the same module), the cost of a function call has to be
more, by a significant amoung, than the cost of the assignment
afterwards.
|
For this particular pair of programs, the code produced in an optimized
build for the main loop was identical, including inlining of the constructor
and destructor. Hence my conclusion that the OP timed unoptimized versions
of the code.
-cd
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Buster Guest
|
Posted: Sun Mar 07, 2004 1:51 am Post subject: Re: why are classes so slow? |
|
|
This was multiposted to c.l.c++. I'm forwarding part of a response
Leor Zolman posted there. I hope Leor doesn't mind. - Buster
Forwarded:
| Quote: | PROG II
class rvector
{
private:
int* vals;
size_t vsize;
public:
explicit rvector( size_t vSize ) { vals = new int[vsize];
|
I'd look real hard at the spelling of those various version of vsize you
have there. What do you think the value of 'vsize' above really is? ;-)
-leor
| Quote: | ~rvector() { delete [] vals; }
int& operator[] (size_t idx ) { return vals[idx]; }
};
void main()
{
rvector v(NUM);
for (i = 0; i < NUM; ++i)
v[i] = i;
}
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thorsten Ottosen Guest
|
Posted: Sun Mar 07, 2004 1:54 am Post subject: Re: why are classes so slow? |
|
|
"James Kanze" <kanze (AT) gabi-soft (DOT) fr> wrote
| Quote: | "Charles Herman" <cherman (AT) no (DOT) spam> writes:
[...]
|> class rvector
[...]
|> int& operator[] (size_t idx ){ return vals[idx]; }
|> };
|> What is happening in program II that takes so much time?
The operator[] is a non-inline function, not a built-in operator.
|
Functions defined in classes are implicitly inline. You mean that
the compiler is not forced to inline it, and probably won't do in
debug builds.
br
Thorsten
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dietmar Kuehl Guest
|
Posted: Sun Mar 07, 2004 10:26 am Post subject: Re: why are classes so slow? |
|
|
Charles Herman wrote:
| Quote: | class rvector
{
private:
int* vals;
size_t vsize;
public:
explicit rvector( size_t vSize ) { vals = new int[vsize]; }
|
Note, that 'vsize' in the allocation is the still unitialized
value of the member variable and not the argument passed to
the function. Please repeat your tests with a fixed
implementation, eg.:
explicit rvector( size_t vSize ):
vsize( vSize )
{ vals = new int[vsize]; }
You cannot initialize 'vals' in the member initializer list,
too, when using 'vsize' because this would cause the same
problem: the initialization in the member initialization
uses the same order as the member declarations in the class,
not the order in the list.
The actual issue is pretty hard to spot. Since I had similar
problems I'm using some prefix to my member variables ('m_')
which helps me tracking problems like this down.
--
<mailto:dietmar_kuehl (AT) yahoo (DOT) com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|