Memory Cost Comparison

May 3, 2017 at 10:12pm
Simple question, which process is more expensive and why?

1
2
3
4
5
6
7
8
9
10
11
  //either
  map<string, map<string, string> > a;

  //or 
  struct obj
  {
    string x;
    string y;
  }
  map<string, obj> b;
  


Thanks.
May 3, 2017 at 11:31pm
Structs without any methods should have a near 0 cost (some OS/compilers (used to?) pad them in weird ways, though, to align the members). Maps have a number of hidden internal variables and things. So I would bet the struct uses less space, marginally. It adds up if you have billions of them.

Last edited on May 3, 2017 at 11:31pm
May 3, 2017 at 11:51pm
Besides the potential difference between the sizeof the two objects, the extra std::maps will incur additional allocations as they are used.
Last edited on May 3, 2017 at 11:52pm
May 4, 2017 at 12:08am
The question is moot; the two options are very different in terms of functionality.

The second is can be written as std::map< std::string, std::pair<std::string,std::string> >
The member and helper functions of std::pair<> provide a lot of convenience without adding any extra overhead over the struct obj
Topic archived. No new replies allowed.