C++: map and hash_map performance

Posted on 2008-11-07
Medium Priority
Last Modified: 2013-12-14
Hi, a general question on map and hash_map:
I am doing a performance enhancement on a program; the program processes a data file every 5 minutes.

Initially the program used the STL map to store the (key, data) pair. But it seems like it slows down after a while (about one day) then picks up the speed after a period of time (after several hours).

Then the hash_map is used since the the hash_map can find the key faster. But then the same behavior happens on the new implementation of using  a hash_map.

So now the quesiton is: does this kind of behavior have somehting to do with the memory allocation? The program (map or hash_map) takes a data file, read the content to set the key and its value; but at some point of time the map or hash_map erases the pair once the key/data is no longer needed.

The only cause I can come out with is that: the allocation of memory is done for evvery new data pair, then when this data pair is erased from the map/hash_map, the memory is not released for a period of time (a lot of (key, data) pairs are added and removed from time to time) so the new memory allocation of new pairs takes a long time (to find a good memory location). At some point of time, the OS comes in to reclaim the memory (from the deleted data pairs) so the process can run fast again.

Not sure this is exactly what happened; so experts please offer your comments on how/what I should do to improve the performance.

Question by:cw1592
  • 2
LVL 21

Expert Comment

ID: 22922367
Use a code profiler to look at performance and easily identify exactly areas could be improved.  Red-gate ants would be an example.

Author Comment

ID: 22922687
BTW, the program is running on a Linux box.

Accepted Solution

cw1592 earned 0 total points
ID: 23293016
some testing shows that the allocation of the value in the pair of (key, value) has the key impact on the performacne; using a memory/data allocation call from the Boost library can be used to improve the perfromace of the STL map.

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question