Are these two setups equal in terms of memory-usage and performance?
I'm currently using singeltons to access different managers in my system. But I've noticed that the singelton Get() functions take a fair chunk of the performence because they are called quite often. (I thought the compiler would optimize that, but apparently not)
So I started looking for ways to change this.
One way was just to put them in a big shared-state class. But I'm still working and modifying these managers. So I want to forward declare them, to avoid recompiling (a full recompile takes about 15 minutes).
But then I though, doesn't 'new' put them in another memory position. Not the same as static declared non-pointer variables.
Question 1: How does these two solutions compare to each other, in terms of performance?
Question 2: And are they faster than a simple singleton-pattern?
////////////////////////////////////////////////////////////
// Way 1: Shared State
////////////////////////////////////////////////////////////
// .h
class SharedState
{
// Just put all managers here
static ObjectManager1 manager1;
static ObjectManager2 manager2;
static ObjectManager3 manager3;
static ObjectManager4 manager4;
// Class things
}
or
////////////////////////////////////////////////////////////
// Way 2, Shared State (With pointers)
////////////////////////////////////////////////////////////
// .h
class SharedState
{
// Just put all managers here
static ObjectManager1* manager1;
static ObjectManager2* manager2;
static ObjectManager3* manager3;
static ObjectManager4* manager4;
}
// .cpp
void Init()
{
manager1 = new ObjectManager1();
manager2 = new ObjectManager2();
// Blah blah
}
Aucun commentaire:
Enregistrer un commentaire