Author Topic: Usage of std::maps  (Read 875 times)

DanielB

  • Newbie
  • *
  • Posts: 17
Usage of std::maps
« on: May 09, 2016, 03:41:53 PM »
Dear all,

I am currently using in my Kratos application a lot of std::maps to get for a specific node Id a corresponding double value. I use something like the following:

    std::map<int,double> m_filtered_dFdX;
    std::map<int,double> m_filtered_dCdX;
    std::map<int,double> m_search_direction;

I noticed std::maps are slowing down my process noticeably. So I want to avoid them. Now I was thinking to simply store all these values on the node by creating dedicated local variables in my application (which then would be quite some). These are no solution variables I just need to update them once in a while. I am however not sure if this is a good way, so I wanted to post the question here: Is it recommended or good practice in such a case to store the values directly on the node using local variables or is there other recommendations?

Thanks a lot in advance!

Best
Daniel

riccardo

  • Global Moderator
  • Newbie
  • *****
  • Posts: 47
Re: Usage of std::maps
« Reply #1 on: May 09, 2016, 04:49:42 PM »
Hi Daniel,

maps inherenetly have a pretty large overhead, which depends on their inner implementation as trees.

if the ordering in which they are stored is not crucial to you tan you may try to use std::unordered_map instead.

having said this, i think that storing the variable as a node data (GetValue(VAR), SetValue(VAR, value)) is a better fit for what you need to do.

cheers
Riccardo

pooyan

  • Global Moderator
  • Newbie
  • *****
  • Posts: 33
Re: Usage of std::maps
« Reply #2 on: May 09, 2016, 11:24:39 PM »
As Riccardo mentioned the maps are very slow and apply large memory overhead.

C++11 provides the unordered_map which is a hash based dictionary.

By the way if they are nodal variables and you should retrieve them I would put them in the nodes.

Bests.

DanielB

  • Newbie
  • *
  • Posts: 17
Re: Usage of std::maps
« Reply #3 on: May 10, 2016, 06:13:58 PM »
Thanks to both of you guys! That helped a lot, also in understanding. I will definitely go for a storage on the nodes in this case.

Best
Daniel