I am looking at an existing piece of code that uses LOT of singleton classes and calls. I am trying to improve the performance of this code.
One thing that comes to mind is optimizing the Singleton :: getInstance () code snippet.
Instead of using Singleton :: getInstance (), I tend to replace this with a structure with two calls.
a. A function that will create and prepare an instance of singleton, for example Singleton :: prepareInstance (), which will be called once at the beginning of the subsystem. b. a built-in implementation of getInstance (), which simply returns the link without checking if it is valid or not.
Is this a viable solution? Any way to improve this?
Current Singleton :: getInstance () code, which looks like
Singleton * Singleton::getInstance() {
if(m_instance == NULL) {
m_instance = new Singleton();
}
return m_instance;
}
Is the approach mentioned by πάντα ῥεῖ any faster?
source
share