c++ - Difference in amount of time it takes to build using G++ -
i practicing building linked list , thought of separating functions separate files , decouple main file.
this file structure came with
./ functions printlist.cpp functionbcd.cpp functions.h linkedlist.cpp node.h
header file in linkedlist.cpp
#include "functions.h" #include <bits/stdc++.h> using namespace std;
header files in functions.h
#include <bits/stdc++.h> #include "node.h"
header files in "any function implemented".cpp
#include <bits/stdc++.h> #include "..\functions.h" using namespace std;
compile command
g++ -ggdb -o2 -std=c++14 linkedlist.cpp functions\*.cpp
now if keep structure mentioned above, compile time 4-5x more structure keep , define functions in 1 file along main.
i unable understand this.
and if there better way structure files , improve compile time, please tell.
thank you.
there's fixed overhead each of files, namely launching actual compiler each of them, including , parsing "common part" (i.e. library includes) , per-file amount of work linker has do.
given actual code wrote minimal, time cost of each of file same, , equivalent fixed overhead; so, behavior seeing not strange.
separating different files starts make sense performance-wise 2 reasons:
- incremental builds; if have large project (with decent build system) , touch single file, it's corresponding object module rebuilt (+link), way faster compiling single huge file every time;
- parallel builds; c++ compilers single thread (which not strange, given of job strictly sequential), compilation of single file cannot exploit parallelism provided current cpus; if split project separate files, problem becomes embarrassingly parallel, given compilation of each tu independent others (the object modules tied @ end linker); so, splitting in multiple files pays off when building on multiple cores.
(this besides obvious maintenability advantages come having separated files different modules/classes)
Comments
Post a Comment