Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > perfomance of clear vs swap

Reply
Thread Tools

perfomance of clear vs swap

 
 
Krishanu Debnath
Guest
Posts: n/a
 
      11-28-2006
Hello,

I have a call to hash_map::clear() function which takes long time.

someClass::someFunction()
{

// typedef hash_map<name_id, uint> Mp;
// Mp p;
// assuming proper namespace, hash function for name_id obj.

p.clear();
}

Above p.clear() takes long time, profiling indicates 'number of bucket
of hash table is large'.

Now, just for sake of experiments, I replaced this 'clear' call with
swap. i.e.

someClass::someFunction()
{

// typedef hash_map<name_id, uint> Mp;
// Mp p;
// assuming proper namespace, hash function for name_is obj.

//p.clear();
Mp tmp;
p.swap(tmp);
}

Now runtime drops significantly, 10 fold less.

What's exactly cause this run time reduction?

Thanks,
Krishanu




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

 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      11-28-2006

Krishanu Debnath wrote:
> Hello,
>
> I have a call to hash_map::clear() function which takes long time.


Hmmm...surprised this made it into moderated. I've taken it out as I
don't like to wait.

hash_map is not standard and is different on every implementation that
implements it. It's hard to say what the reduction in speed is caused
by.

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      11-28-2006
Noah Roberts wrote:
> Krishanu Debnath wrote:
> > Hello,
> >
> > I have a call to hash_map::clear() function which takes long time.

>
> Hmmm...surprised this made it into moderated. I've taken it out as I
> don't like to wait.
>
> hash_map is not standard and is different on every implementation that
> implements it. It's hard to say what the reduction in speed is caused
> by.


But it is part of TR1 as std::tr1::unordered_map, which qualifies under
FAQ 5.9.

Cheers! --M

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      11-28-2006
Krishanu Debnath wrote:
> I have a call to hash_map::clear() function which takes long time.
>
> someClass::someFunction()
> {
>
> // typedef hash_map<name_id, uint> Mp;
> // Mp p;
> // assuming proper namespace, hash function for name_id obj.
>
> p.clear();
> }
>
> Above p.clear() takes long time, profiling indicates 'number of bucket
> of hash table is large'.
>
> Now, just for sake of experiments, I replaced this 'clear' call with
> swap. i.e.
>
> someClass::someFunction()
> {
>
> // typedef hash_map<name_id, uint> Mp;
> // Mp p;
> // assuming proper namespace, hash function for name_is obj.
>
> //p.clear();
> Mp tmp;
> p.swap(tmp);
> }
>
> Now runtime drops significantly, 10 fold less.
>
> What's exactly cause this run time reduction?


I'm not sure why this would be. Are you using SGI's hash_map extension
(which is similar to std::tr1::unordered_map)? Are you measuring the
performance of the entire function including the destructors for
automatic objects like tmp?

Cheers! --M


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

 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      11-28-2006

mlimber wrote:
> Noah Roberts wrote:
> > Krishanu Debnath wrote:
> > > Hello,
> > >
> > > I have a call to hash_map::clear() function which takes long time.

> >
> > Hmmm...surprised this made it into moderated. I've taken it out as I
> > don't like to wait.
> >
> > hash_map is not standard and is different on every implementation that
> > implements it. It's hard to say what the reduction in speed is caused
> > by.

>
> But it is part of TR1 as std::tr1::unordered_map, which qualifies under
> FAQ 5.9.


But hash_map is not unordered_map. Read the first part on unordered
containers in TR1 that explains exactly why the name hash_map was _not_
used....because that name is already taken in most implementations by
incompatible and disparate versions of a hash interface.

I doubt that is the topic of discussion or the OP probably would have
used the TR1 name. More likely they are working with some
implementation defined interface.

 
Reply With Quote
 
Krishanu Debnath
Guest
Posts: n/a
 
      11-28-2006
mlimber wrote:
> Krishanu Debnath wrote:
>> I have a call to hash_map::clear() function which takes long time.
>>
>> someClass::someFunction()
>> {
>>
>> // typedef hash_map<name_id, uint> Mp;
>> // Mp p;
>> // assuming proper namespace, hash function for name_id obj.
>>
>> p.clear();
>> }
>>
>> Above p.clear() takes long time, profiling indicates 'number of bucket
>> of hash table is large'.
>>
>> Now, just for sake of experiments, I replaced this 'clear' call with
>> swap. i.e.
>>
>> someClass::someFunction()
>> {
>>
>> // typedef hash_map<name_id, uint> Mp;
>> // Mp p;
>> // assuming proper namespace, hash function for name_is obj.
>>
>> //p.clear();
>> Mp tmp;
>> p.swap(tmp);
>> }
>>
>> Now runtime drops significantly, 10 fold less.
>>
>> What's exactly cause this run time reduction?

>
> I'm not sure why this would be. Are you using SGI's hash_map extension


Yes. I am using g++ 4.0.2, which adopts SGI's implementation, I believe.

> (which is similar to std::tr1::unordered_map)? Are you measuring the
> performance of the entire function including the destructors for
> automatic objects like tmp?


Yes.

Krishanu


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

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      11-28-2006
Noah Roberts wrote:
> mlimber wrote:
> > Noah Roberts wrote:
> > > Krishanu Debnath wrote:
> > > > Hello,
> > > >
> > > > I have a call to hash_map::clear() function which takes long time.
> > >
> > > Hmmm...surprised this made it into moderated. I've taken it out as I
> > > don't like to wait.
> > >
> > > hash_map is not standard and is different on every implementation that
> > > implements it. It's hard to say what the reduction in speed is caused
> > > by.

> >
> > But it is part of TR1 as std::tr1::unordered_map, which qualifies under
> > FAQ 5.9.

>
> But hash_map is not unordered_map. Read the first part on unordered
> containers in TR1 that explains exactly why the name hash_map was _not_
> used....because that name is already taken in most implementations by
> incompatible and disparate versions of a hash interface.


Right, but it is also similar enough to the TR1 container to fall
within the bounds of this group, IMHO (and apparently in that of the
moderator of c.l.c++.m). This question may simply be a QOI issue for a
container that is quite close to TR1's, or it may be an issue of misuse
of the container or faulty measurements. In any of these cases, at
least initially I think it is falls within the (fuzzy) boundaries for
this group, whereas if it is an issue of a non-standard container that
is unlike unordered_map but with the same name, then it is almost
certainly outside the bounds of this group. Given the information in
the OP, however, I don't think we can determine for certain which of
these it is, and so ISTM that we should give it the benefit of the
doubt.

Cheers! --M

 
Reply With Quote
 
Aaron Graham
Guest
Posts: n/a
 
      11-29-2006
> I have a call to hash_map::clear() function which takes long time.
[...]
> Now, just for sake of experiments, I replaced this 'clear' call with
> swap.

[...]
> Now runtime drops significantly, 10 fold less.
> What's exactly cause this run time reduction?


Because the two functions do different amounts of work.

clear() requires time that is linear with respect to the number of
elements in the container. Destructors are called, memory is
deallocated, etc. But swap() can be done by swapping a few pointers.

Aaron


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

 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      11-29-2006

Aaron Graham wrote:
> > I have a call to hash_map::clear() function which takes long time.

> [...]
> > Now, just for sake of experiments, I replaced this 'clear' call with
> > swap.

> [...]
> > Now runtime drops significantly, 10 fold less.
> > What's exactly cause this run time reduction?

>
> Because the two functions do different amounts of work.
>
> clear() requires time that is linear with respect to the number of
> elements in the container. Destructors are called, memory is
> deallocated, etc. But swap() can be done by swapping a few pointers.


Yes, but it seems that swapping with an empty temporary and then
deleting it would also call the same destructors and such that clear
would.

 
Reply With Quote
 
Seungbeom Kim
Guest
Posts: n/a
 
      11-29-2006
Aaron Graham wrote:
>> I have a call to hash_map::clear() function which takes long time.

> [...]
>> Now, just for sake of experiments, I replaced this 'clear' call with
>> swap.

> [...]
>> Now runtime drops significantly, 10 fold less.
>> What's exactly cause this run time reduction?

>
> Because the two functions do different amounts of work.
>
> clear() requires time that is linear with respect to the number of
> elements in the container. Destructors are called, memory is
> deallocated, etc. But swap() can be done by swapping a few pointers.


That's true for swap() alone, but what about the destructor of the
"temporary" variable (tmp in the OP's code) when it goes out of scoppe?
Doesn't it do the same things, including destruction and deallocation?

--
Seungbeom Kim

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What swap is called when using std::swap? Niels Dekker (no reply address) C++ 4 07-20-2006 08:44 PM
CreateInstranceAndUnwrap slow perfomance Gnanaprakash Rathinam ASP .Net 5 12-30-2004 09:18 PM
Server perfomance Fredrik Melin ASP .Net 1 10-27-2004 11:45 AM
perfomance an ip-route-cache on a router Ralf Huelsmann Cisco 1 08-15-2004 03:09 PM
Optimizing perfomance on T3 line Christoph Schad Cisco 12 01-01-2004 08:40 PM



Advertisments