C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Unexpected Performance

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Joe Chen
Guest





PostPosted: Fri Jul 16, 2004 1:33 pm    Post subject: Unexpected Performance Reply with quote



Hello,

I recently stumbled upon timings I didn't expect, and I would be
grateful if you could help me understand what's happening here. I have
the following program written and run under gcc 3.3 under Fedora with
full optimization:

--- BEGIN ---

#include <iostream>
#include <list>
#include <unistd.h>
#include <time.h>

using namespace std;

class C {
public:
C() {
for ( unsigned int i=0; i<1000; ++i ) {
array[i] = i;
}
}

double array[1000];
};

int main()
{
unsigned int iterations = 10000;
double t, t2;

clock_t start = clock();

list
for ( unsigned int i=0; i<iterations; ++i ) {
value_list.push_back( C() );
}

clock_t finish = clock();

t = ( finish - start ) / static_cast
clock_t start2 = clock();


list<C*> pointer_list;

for ( unsigned int i=0; i<iterations; ++i ) {
pointer_list.push_back( new C );
}

clock_t finish2 = clock();

t2 = ( finish2 - start2 ) / static_cast
for ( list<C*>::iterator i=pointer_list.begin();
i!=pointer_list.end(); ++i ) {
delete *i;
}
cout << "Time by value: " << t << " sec." << endl;
cout << "Time by pointer: " << t2 << " sec." << endl;
}

---- END ---

I end up with the following output:
Time by value: 2.09 sec.
Time by pointer: 2.24 sec.

I was expecting the list of copied pointers to be faster to create
than the list created by copying the entire value. I assume the
compiler is able to optimize somthing when I am passing by value, but
I wouldn't have assumed it would be that quick.

Is that in fact what is happening here, or is there something wrong
with my test?

Thanks!
Joe

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Victor Bazarov
Guest





PostPosted: Sat Jul 17, 2004 10:02 am    Post subject: Re: Unexpected Performance Reply with quote



Joe Chen wrote:
Quote:
I recently stumbled upon timings I didn't expect, and I would be
grateful if you could help me understand what's happening here. I have
the following program written and run under gcc 3.3 under Fedora with
full optimization:

{quoted code snipped -mod}

I end up with the following output:
Time by value: 2.09 sec.
Time by pointer: 2.24 sec.

I was expecting the list of copied pointers to be faster to create
than the list created by copying the entire value. I assume the
compiler is able to optimize somthing when I am passing by value, but
I wouldn't have assumed it would be that quick.

Is that in fact what is happening here, or is there something wrong
with my test?

Nothing is wrong. Templates at their best. When you push_back by value,
the list creates another object (using 'Allocator::allocate', which most
likely uses 'new', not surprisingly), and constructs it in place. Since
the standard container member functions are usually small, they are very
likely inlined. So, your temporary is probably used directly to init the
object in 'new'. The creation of a temporary in that case can be foregone
(it's allowed by the Standard). So, you default-construct something right
into the object created by the 'new'.

So, what's different in the second case? The list of pointers cannot
forgo creation of another pointer before linking it into the chain of
contained objects. It creates it using 'Allocator::allocate', which is
probably also just a call to 'new'. So, what do you have? You have twice
the calls to 'new' than in the first case.

Considering the optimisation of a temporary object creation in the first
case and twice the dynamic object allocation in the second case, it all
can be justified, I believe.

Victor

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Maxim Yegorushkin
Guest





PostPosted: Sat Jul 17, 2004 10:12 am    Post subject: Re: Unexpected Performance Reply with quote



Joe Chen wrote:

Quote:
--- BEGIN ---

#include <iostream
#include #include #include
using namespace std;

class C {
public:
C() {
for ( unsigned int i=0; i<1000; ++i ) {
array[i] = i;
}
}

double array[1000];
};

int main()
{
unsigned int iterations = 10000;
double t, t2;
clock_t start = clock();

list for ( unsigned int i=0; i<iterations; ++i ) {
value_list.push_back( C() );
}
clock_t finish = clock();

t = ( finish - start ) / static_cast
clock_t start2 = clock();


list<C*> pointer_list;
for ( unsigned int i=0; i<iterations; ++i ) {
pointer_list.push_back( new C );
}
clock_t finish2 = clock();

t2 = ( finish2 - start2 ) / static_cast
for ( list<C*>::iterator i=pointer_list.begin();
i!=pointer_list.end(); ++i ) {
delete *i;
}
cout << "Time by value: " << t << " sec." << endl;
cout << "Time by pointer: " << t2 << " sec." << endl;
}

---- END ---

I end up with the following output:
Time by value: 2.09 sec.
Time by pointer: 2.24 sec.

I was expecting the list of copied pointers to be faster to create
than the list created by copying the entire value.

It's rather contentious because the list itself allocates memory for its nodes. So, when using list you always have +1 memory allocation.

[]

Quote:
Is that in fact what is happening here, or is there something wrong
with my test?

You are measuring rather desperate things here. The first test makes one memory allocation and most likely one copy of C. The second test makes
two memory allocations (the first is new C, the second is for the node of list<>) and no copy of C. Given the definition of C it looks like
copying it may take considerable amount of time which renders your test results meaningless.

I presume you are measuring here performance of the list<C> and list<C*>. To make the results make some sense try making your value class (C)
stateless (i.e. no data members).

--
Maxim Yegorushkin

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Kendrick Whitegoat
Guest





PostPosted: Sat Jul 17, 2004 10:20 am    Post subject: Re: Unexpected Performance Reply with quote

Joe Chen wrote:
Quote:
I recently stumbled upon timings I didn't expect, and I would be
grateful if you could help me understand what's happening here. I have
the following program written and run under gcc 3.3 under Fedora with
full optimization:
[...]
I end up with the following output:
Time by value: 2.09 sec.
Time by pointer: 2.24 sec.

I was expecting the list of copied pointers to be faster to create
than the list created by copying the entire value. I assume the
compiler is able to optimize somthing when I am passing by value, but
I wouldn't have assumed it would be that quick.

Is that in fact what is happening here, or is there something wrong
with my test?

Try swapping the order of the value/pointer code or put a block around
the value_list code and timing again, perhaps you are just getting hit
by memory swapping towards the end?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Antoun Kanawati
Guest





PostPosted: Sun Jul 18, 2004 10:50 am    Post subject: Re: Unexpected Performance Reply with quote

Kendrick Whitegoat wrote:

Quote:
Joe Chen wrote:
I recently stumbled upon timings I didn't expect, and I would be
grateful if you could help me understand what's happening here. I have
the following program written and run under gcc 3.3 under Fedora with
full optimization:
[...]
I end up with the following output:
Time by value: 2.09 sec.
Time by pointer: 2.24 sec.

I was expecting the list of copied pointers to be faster to create
than the list created by copying the entire value. I assume the
compiler is able to optimize somthing when I am passing by value, but
I wouldn't have assumed it would be that quick.

Is that in fact what is happening here, or is there something wrong
with my test?

Try swapping the order of the value/pointer code or put a block around
the value_list code and timing again, perhaps you are just getting hit
by memory swapping towards the end?

I would start by writing the copy ctor and operator= of the class C.
Then, I would instrument them lightly to count how often each ctor and
operator= is called. If you're lucky, the performance will remain
similar, and you'll have numbers to help you figure it out.

If that doesn't work out, generate the assembly code and look at it to
figure out what is going on when you're populating list<C>.

To summarize, we have the following:

struct C {
double d[1000];
C() { for (int i = 0; i < 1000; i++) d[i] = };
typedef list<C> CList; CList clist;
typedef list<C*> PList; PList plist;

The performance numbers compare repeated invocations of
clist.push_back(C()) to the same number of plist.push_back(new C).
--
Antoun Kanawati
[email]antounk.at (AT) comcast (DOT) dot.net[/email]
[remove .dot and .at before use]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Joe Chen
Guest





PostPosted: Sun Jul 18, 2004 11:01 am    Post subject: Re: Unexpected Performance Reply with quote

I tried your suggestion, and in fact the timings for whichever version
was second were always greater. I've separated the test into two
separate executables, and this time I obtained timings that were more
in line with what I expected. Apparently, my problem was memory
swapping during the second test.

Kendrick Whitegoat <kwhitegoat (AT) spymac (DOT) com> wrote


Quote:
Try swapping the order of the value/pointer code or put a block around
the value_list code and timing again, perhaps you are just getting hit
by memory swapping towards the end?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.