Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will folders and namespaces affect performance in C++ and cross-platform?

A question was asked about whether namespace and folder structure would affect performance of an assembly in C#. The answers were very useful, but where specific to C# and the CLR.

How will the namespace and folder structure affect the performance of an assembly if it is written in C++ with gcc? What's the situation on other OSes, such as Linux or Mac OS?

If there are any significant performance issues, what should I do or avoid doing to maximize performance?

like image 641
The Floating Brain Avatar asked Feb 17 '12 02:02

The Floating Brain


2 Answers

Neither your directory hierarchy nor namespaces will affect your compiled code. The code your compiler will generate will be the same. This goes for all compilers and all OS's.

like image 58
Kyle Avatar answered Sep 21 '22 03:09

Kyle


To expand a bit on what Kyle said:

Namespaces are nothing more than a syntactic way for the user and the compiler to put names in different buckets. They exist to allow you to use more common and appropriate names for things without having to worry about conflicts with someone else. std::vector is a different type from a mathematical vector class. They can share the same name so long as they're in different namespaces.

As far as the compiler is concerned, a function in a namespace is no different from a function anywhere else. It just has a funny name. Indeed, compilers are allowed the freedom to do what's called "name mangling": when the compiler sees std::vector<int>, it can actually convert that into something like __std~~vector~t~~int32_t~~__ or whatever. The mangling algorithm is chosen so that no user-defined name in the global namespace can match the name used by the namespace mangler. Thus all namespace-scoped names are separate from names in other namespaces, even the global one.

Basically, the first step in the compilation process is to effectively eliminate namespaces. Therefore, later compiler steps have no clue what namespace something is even in. So they cannot generate code from it. And thus, namespaces cannot have any effect on the execution speed of compiled code.

Folders... cannot possibly matter. After compilation, you get a single executable, library, or DLL. If a compiler ever did any code generation based on the location of the source files, you would be well advised to avoid that compiler like the plague. The compiler writers would have to be trolling their users to make that happen.

like image 45
Nicol Bolas Avatar answered Sep 20 '22 03:09

Nicol Bolas