0
The cost of access to unordered_map
can cause FPS to drop in a game if it is accessed thousands of times per second?
class var
{
public:
template < typename t = double >
static inline auto get( const std::string& var_name ) -> t&
{
return ( t& ) m_vars[ var_name ];
}
private:
static inline std::unordered_map< std::string, double > m_vars;
};
// Isso vai ficar rodando infinitamente em vários loops
// Não vai ser apenas uma vez que vou chamar o método get, vou chamar varias e varias vezes em vários loops diferente
// Acredito que o acesso ao map vai comprometer muito o desempenho do jogo ocasionando perda de fps
if ( var::get< bool >( "ativar_efeitos" ) )
{
}
This access will happen while the game is running, all the function that needs to use a variable I would use the get function instead of the variable because in my view it is much more organized than filling the project with global variables or putting everything inside a namespace. About the hash function I am using a library(cx_fnv1) to hash the string, in my tests it got much faster. And why do you think the code shouldn’t be written like that? I’m doing something wrong?
– cYeR
Do not use global variables. Use variables in a specific scope, but use variables, not a map, they are not created for this. I cannot give details without knowing the whole, but a class that only has an empty map and a
get()
seems wrong, the class name seems wrong, the generalization seems wrong. But from what I’ve commented now, everything seems wrong.– Maniero
The class is not complete yet I have not finished writing, yet I intend to create methods to save/load the variable in a file, why I think creating a class with a map would be ideal as I could save/read all values of all variables with a simple for each.
– cYeR
he is.... No....
– Maniero
Could you indicate a better alternative? The only thing I can think of that meets my needs at the moment would be a map.
– cYeR
Make a good static class, there’s no need to create a map. Or do it in Ruby, Lua, another language. If you’re going to do it in C++ use the C features++.
– Maniero