Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Repetitive indexing a std::map with same index

Reply
Thread Tools

Repetitive indexing a std::map with same index

 
 
Urs Thuermann
Guest
Posts: n/a
 
      11-03-2011
I have a case where I must access a std::map using operator[] where
there is a high probability that the same index is used repetitively
for a number of times. Will I improve performance when I remember the
last index used and compare each time I access the map, or are map
implementations usually optimized for this case?

I am thinking of something like this (much simplified):

class Debug {
pthread_t last_tid;
struct LineBuffer *last_buf, *line_buf;
std::map<pthread_t, LineBuffer> map;

template <class T>
Debug &operator<<(const T &a) {
pthread_t tid = pthread_self();
if (tid == last_tid) {
line_buf = last_buf;
} else {
line_buf = &map[tid];
last_tid = tid;
}

// write 'a' to the line_buf,
// flush to cerr when complete.
;

return *this;
}
};

so multiple threads can write

Debug() << "foo value is " << foo << " yadda yadda" << std::endl;

without having their output mangled with other debug messages.

urs
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-03-2011
On 11/3/2011 10:00 AM, Urs Thuermann wrote:
> I have a case where I must access a std::map using operator[] where
> there is a high probability that the same index is used repetitively
> for a number of times. Will I improve performance when I remember the
> last index used and compare each time I access the map, or are map
> implementations usually optimized for this case?


Maybe. But at what cost? How is your cache invalidated?

> I am thinking of something like this (much simplified):
>
> class Debug {
> pthread_t last_tid;
> struct LineBuffer *last_buf, *line_buf;
> std::map<pthread_t, LineBuffer> map;
>
> template<class T>
> Debug&operator<<(const T&a) {
> pthread_t tid = pthread_self();
> if (tid == last_tid) {
> line_buf = last_buf;
> } else {
> line_buf =&map[tid];
> last_tid = tid;
> }
>
> // write 'a' to the line_buf,
> // flush to cerr when complete.
> ;
>
> return *this;
> }
> };
>
> so multiple threads can write
>
> Debug()<< "foo value is "<< foo<< " yadda yadda"<< std::endl;
>
> without having their output mangled with other debug messages.


I suppose there *can* exist an implementation that caches the last
accessed value since it's in the same object that should know when the
cached value needs to be thrown out. I haven't seen one myself yet.
And I probably would recommend against implementing such a caching
mechanism outside of 'std::map' since your object does not know what
happens to the map element in between calls. What if it was removed
from the map? The pointer is invalidated, and your object hasn't been
notified...

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Jorgen Grahn
Guest
Posts: n/a
 
      11-03-2011
On Thu, 2011-11-03, Urs Thuermann wrote:
> I have a case where I must access a std::map using operator[] where
> there is a high probability that the same index is used repetitively
> for a number of times. Will I improve performance when I remember the
> last index used and compare each time I access the map, or are map
> implementations usually optimized for this case?


"Measure" or "read your implementation" seem like valid replies.

I bet there is no such optimization in real std::map implementations,
unless you count the positive effect on the data cache of traveling
the same path through the tree over and over again.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
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
Block repetitive connection from the same ip Masterx81 Cisco 2 11-26-2007 02:09 PM
Indexing services under Windows XP SP2 - Can I disable MS Indexing Service to hasten Google's OR does Google Desktop uses this MS Indexing Service? ricardodefaria Computer Support 6 08-05-2007 04:14 AM
How much slower is dict indexing vs. list indexing? Emin Python 4 01-12-2007 02:40 PM
Looping almost the same repetitive lines Nikos Perl Misc 11 04-23-2005 07:12 AM
Indexing PDF Files using MS Indexing Service C ASP .Net 0 10-17-2003 04:27 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57