On Apr 30, 3:09 am, devdude <rottygu...@[EMAIL PROTECTED]
> wrote:
> I have the need to take a snapshot of a hash_map during execution
> (actually transform it to a vector). This map is a shared resource
> and therefore must be locked prior to any read/write operations thus I
> need to minimize the amount of time the map resource is locked.
>
> The map is defined as type <string, boost::shared_ptr<myobject>>. My
> algorithm is as such:
>
> void SnapShotToVector( vector< pair< string,
> boost::shared_ptr<myobject> >& vec )
> {
>
> lockResource(this->map);
> vec.resize( map.size() );
> copy(this->map.begin(), this->map.end(),list.begin());
> unlockResource(this->map);
>
> }
>
> For about 3M elements w/in the map, I'm noticing that the resize op
> takes about 150ms and the copy takes ~850ms. Is there any way to do
> better? I suppose the total time doesn' t matter as it's the time the
> resource is actually locked is the primary concern.
One slightly bizarre suggestion: if you don't literally need a
snapshot you may be able to break the copying into a set of:
1. lock
2. copy up to N elements
3. unlock
4. if you've copied everything, throw a party and forget about 5&6
5. lock
6. search for the last key copied
if you find it, from that starting position, go to 2
else delete the key from your copy, go to 6
(if you exhaust the copied keys, go to 2)
This relies on the observation that it's quick to get an iterator back
in to a known element.
Of course, there are many good reasons for wanting a fair-dinkum
snapshot, so apologies if this is applicable to your situation.
Cheers,
Tony
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|