0

I would like to define several global constant values which will be used across multiple .cpp files. It is not essential to know the values at compile time.

One option is to define, e.g. const double some_constant =4.1; in a header file and include it in my various .cpp files. (This seems like the most natural option, but I am a complete beginner.)

Another option is to put extern const double some_constant; in the header file and set it as const double some_constant=4.1; in a separate .cpp file, then include the header files as before. (I have heard that this allows the compiler to make better optimizations, but I don't understand why.)

Or, as mentioned in an answer to this post, a third possibility is to define

constexpr double some_constant {4.1};

Is there actually any difference between these options in terms of performance/allowing the compiler make the best optimizations? I am using g++ with the -O3 flag if that is relevant.

user366202
  • 249
  • 2
  • 7
  • I prefer the `constexpr` approach, but I have no hard measurements to back my preference. – user4581301 Jun 10 '20 at 20:52
  • 1
    What compiler and version do you use? Is it [GCC](http://gcc.gnu.org/)? then use `g++ -Wall -Wextra -g -O3` and learn about the `-flto` GCC flag. Are you willing to spend weeks of your time to win less than a percent of runtime performance? Did you try [Clang](http://clang.llvm.org/) ? Did you benchmark your program ? Did you read the documentation of your compiler, of your profiler (e.g. [gprof](https://www.thegeekstuff.com/2012/08/gprof-tutorial/) ?) Your question is opinion-based, you don't explain what is "best" ! – Basile Starynkevitch Jun 10 '20 at 20:56
  • @BasileStarynkevitch I thought this might be an 'easy' way to improve the speed of my code in a couple of minutes if one of these methods was known to be more efficient (along the same lines as how declaring relevant values as const, and switching on optimizations during compilation improves performance). But if it only makes a marginal difference to performance of less than a couple of percent, then it is not so important. I will try enabling `-flto` anyway to see if this makes any improvement. – user366202 Jun 10 '20 at 21:16
  • Read [*Introduction to Algorithms*](https://en.wikipedia.org/wiki/Introduction_to_Algorithms) then a good [C++ programming](https://stroustrup.com/programming.html) book, [this C++ reference](https://en.cppreference.com/w/cpp) and the documentation of your C++ compiler, in particular [this chapter for GCC](https://gcc.gnu.org/onlinedocs/gcc/Invoking-GCC.html). **There is often no *easy* way to improve performance**. See [this draft report](http://starynkevitch.net/Basile/bismon-chariot-doc.pdf) and http://norvig.com/21-days.html – Basile Starynkevitch Jun 11 '20 at 05:33
  • Read also a good book on [*Linkers and loaders*](https://www.iecc.com/linker/) and the [*Dragon book*](https://en.wikipedia.org/wiki/Compilers:_Principles,_Techniques,_and_Tools) – Basile Starynkevitch Jun 11 '20 at 05:39

0 Answers0