简体   繁体   中英

Declaring/defining c++ struct in different file - better compile time using gcc 4.8.5?

I currently have a c++ code and a Json file. Json file contains enumerations in 2-D structure, so every outer key in Json has a map as its value ie {Outer_key : {{Inner_key : Inner_value}, ...}, ...}. C++ code contains overloaded print function which parses the input data, and in the process of the function call, the code fetches Inner_value using Outer_key and Inner_key. For each call of the c++ main function, around 0~10 Inner_values are retrieved; however, the entire Json file maps about ~20,000 Inner_values.

I am using python to create the c++ code, and am compiling using gcc (CMAKE). I need to keep some kind of enumeration map within the body of c++ so I can run c++ code, get intermediate integer value and pass it into enumerations to finally return the associated string.

Right now, I list-initialize a 2-D unordered_map in the main function of the c++ file. This takes the shortest time among all the other compile-time initializations; however, it still takes 5~10 minutes.

On suggestion I received is to divide the 2-D enumerations into multiple(total number of Outer_keys) 1-D structs, store them in a different file, then 'use' a specific 1-D struct when needed.

Two questions I have here.

  1. Even if I divide them up, and put them into different files, doesn't the time to compile remain the same?

  2. If the compile time is reduced by splitting up in multiple 1-D structs, what approach should I take in coding this? Should I declare structs in .h then call them in .cpp main()? Should I go ahead and define the structs in additional .cpp file? Should I just typedef enums? Also, within the main function or the print function, how can I initialize only the struct that I need?

.cpp file generated using python below:

void overLoadedPrint (Particular_Datatype *data, std::unordered_map<std::string, std::unordered_map<std::string, std::string>> enumMap) {

   printf("%s", enumMap["SomeKey"][A->member1.innerMember1].c_str());

   //A->member1.innerMember1 returns integer.
   //"SomeKey" is known in python so corresponding key is inputted.
}

int main() {
   std::unordered_map<std::string, std::unordered_map<std::string, std::string>> enumMap = {{"A", {{"1", "a"},{"2", "b"}}...}
   //list-initalize enumMap.
   //compile time significantly increases here.
   //info of this map is stored in a single json file.

   overLoadedPrint(someData, enumMap);

   return 0;
}

  1. Even if I divide them up, and put them into different files, doesn't the time to compile remain the same?

Splitting a translation unit into fragments usually increases compilation time from scratch.

However, each translation unit can be compiled separately, so if you change only one, then only that file need to be recompiled. Having to compile a fraction of a program is usually much faster than compiling it entirely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM