c++ - static variables slowing launch time of app -
i making cpp application. have created .h file lots of static variables like
namespace msg { static const int msg_type = 1; // many more here strings, ints, chars. } this slowing downwards launch time.
i have used these variables in lot of places.
what can improve speed while reducing amount of effort made in changing code ?
improving performance priority, though.
the fundamental problem (assuming slowing down, rather surprise me) have instance of variable in each translation unit includes header. int, isn't issue, if of types have constructors, be. in general, except integral constants, should not have static in header. (and aware if object const, static default.) thus, string, should be:
namespace msg { extern std::string const somemessage; }; and in source file:
std::string const msg::somemessage( "whatever" ); (the source file should, of course, include header.)
note integral constants, have careful; if utilize doesn't result in immediate lvalue rvalue conversion, need actual info declaration. in every file uses in way, since there 1 instance per file.
a perhaps improve solution here utilize class, rather namespace:
class msg { public: static int const msgtype = 1; static std::string const somemessage; // ... }; this ensures there 1 actual instance, still allows integral constants work integral constant expressions. (here too, need single definition in source file somewhere.)
c++ c static const
No comments:
Post a Comment