Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 72965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:07:38+00:00 2026-05-10T20:07:38+00:00

I’m using a std::map (VC++ implementation) and it’s a little slow for lookups via

  • 0

I’m using a std::map (VC++ implementation) and it’s a little slow for lookups via the map’s find method.

The key type is std::string.

Can I increase the performance of this std::map lookup via a custom key compare override for the map? For example, maybe std::string < compare doesn’t take into consideration a simple string::size() compare before comparing its data?

Any other ideas to speed up the compare?

In my situation the map will always contain < 15 elements, but it is being queried non stop and performance is critical. Maybe there is a better data structure that I can use that would be faster?

Update: The map contains file paths.

Update2: The map’s elements are changing often.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:07:39+00:00Added an answer on May 10, 2026 at 8:07 pm

    First, turn off all the profiling and DEBUG switches. These can slow down STL immensely.

    If that’s not it, part of the problem may be that your strings are identical for the first 80-90% of the string. This isn’t bad for map, necessarily, but it is for string comparisons. If this is the case, your search can take much longer.

    For example, in this code find() will likely result in a couple of string compares, but each will return after comparing the first character until ‘david’, and then the first three characters will be checked. So at most, 5 characters will be checked per call.

    map<string,int> names; names['larry'] = 1; names['david'] = 2; names['juanita'] = 3;  map<string,int>::iterator iter = names.find('daniel'); 

    On the other hand, in the following code, find() will likely check 135+ characters:

    map<string,int> names; names['/usr/local/lib/fancy-pants/share/etc/doc/foobar/longpath/yadda/yadda/wilma'] = 1; names['/usr/local/lib/fancy-pants/share/etc/doc/foobar/longpath/yadda/yadda/fred'] = 2; names['/usr/local/lib/fancy-pants/share/etc/doc/foobar/longpath/yadda/yadda/barney'] = 3;  map<string,int>::iterator iter = names.find('/usr/local/lib/fancy-pants/share/etc/doc/foobar/longpath/yadda/yadda/betty'); 

    That’s because the string comparisons have to search deeper to find a match since the beginning of each string is the same.

    Using size() in your comparison for equality won’t help you much here since your data set is so small. A std::map is kept sorted so its elements can be searched with a binary search. Each call to find should result in less than 5 string comparisons for a miss, and an average of 2 comparisons for a hit. But it does depend on your data. If most of your path strings are of different lengths, then a size check like Motti describes could help a lot.

    Something to consider when thinking of alternative algorithms is how many many ‘hits’ you get. Are most of your find() calls returning end() or a hit? If most of your find()s return end() (misses) then you are searching the entire map every time (2logn string compares).

    Hash_map is a good idea; it should cut your search time in about half for hits; more for misses.

    A custom algorithm may be called for because of the nature of path strings, especially if your data set has common ancestry like in the above code.

    Another thing to consider is how you get your search strings. If you are reusing them, it may help to encode them into something that is easier to compare. If you use them once and discard them, then this encoding step is probably too expensive.

    I used something like a Huffman coding tree once (a long time ago) to optimize string searches. A binary string search tree like that may be more efficient in some cases, but its pretty expensive for small sets like yours.

    Finally, look into alternative std::map implementations. I’ve heard bad things about some of VC’s stl code performance. The DEBUG library in particular is bad about checking you on every call. StlPort used to be a good alternative, but I haven’t tried it in a few years. I’ve always loved Boost too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can store any object in a SharedObject, but you… May 11, 2026 at 11:33 pm
  • Editorial Team
    Editorial Team added an answer SSL is very complex, so you're going to want to… May 11, 2026 at 11:33 pm
  • Editorial Team
    Editorial Team added an answer I moved the instantiation of DataAccessLayer out of the constructor… May 11, 2026 at 11:33 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.