-1

I am confused to choose between the two methods to have a STL structure ,

Method A:

map<pair<string,int>,map<string,map<ULONG,vector<string>>*>*>

Method B:

Is the above advisable or having a separate maps like below,

map<pair<string,int>,vector<string>>

After querying from this parent map , then iterating the vector and query the second map

map<string,map<ULONG,vector<string>>*>

Out of the above two methods which is the optimal way and which will cause more performance overhead?

Update 1:

My target is to store the output logs in memory which has three groups.. the outermost key "pair" is parent grouping and which has it's own sub groups.. And each sub groups will have it's own groups.

After TypeDef the Method A:

  typedef map<ULONG,vector<string>> Sub_Map2;
   typedef map<string,Sub_Map2*> Sub_Map1;
   typedef map<pair<string,int>,Sub_Map1*> Parent_map;

For better readability

Sel_va
  • 588
  • 5
  • 25

2 Answers2

2

Don't go with premature optimization. Use clean code and try to optimize it only if you see a bottleneck in that code. Use typedef's in order to maintain readability.

I.e. (I don't know how you want to organize it).

typedef map<ULONG, vector<string>> IDLogMap;
typedef map<pair<string, int>, IDLogMap> PairLogMap;

Anyway I suggest you to refactor a bit your code, creating some log message class and so on, because map<pair<string,int>,map<string,map<ULONG,vector<string>>*>*> it's a bit too complicated for me, especially if you want to obtain a specific log message. Also, try to avoid raw pointers.

Jepessen
  • 11,744
  • 14
  • 82
  • 149
  • Three sublevels for a logger seems a bit too much. And why the `std::pair`? – Jepessen Sep 21 '15 at 10:35
  • that is a pair of ip and port – Sel_va Sep 21 '15 at 10:48
  • 1
    Then create a `MessageLog` class, putting in it the message log and its levels like (DEBUG, 1). Create also a class representing address and ip, and use it inside the map. Maybe you should use `multimap` in order to set different log messages to same address/ip. – Jepessen Sep 21 '15 at 14:50
0

std::map will allocate each key-value pair separately, so there is no reallocation done when you insert or remove elements from the maps. Meaning, there is no overhead difference between the two versions (asides from the extra lookup).

That said, option B may be nicer if you ever need to iterate the inner maps on their own - if you don't, no need to complicate the code.

Tino Didriksen
  • 2,215
  • 18
  • 21