0

My question is simple, what is the performance loss due to reference length. I cannot explain myself but here is the sample:

between this

C* pC = m_a->m_b->m_c;

and this expression

C* pC = m_b->m_c;

I am asking this because I have a global class which has a Singleton pattern and holds everything. I am accessing all of its members from its members like this.

class Global
{
    A* a;
    X* x;
};

class A { B* b; };
class B { C* c; }; // etc

class X { Y* y; };
class Y { Z* z; };
class Z
{
    void foo() { Global::GetInstance()->a->b->c->foo(); }
}

Is this a good design? Any advice for this? I am having some trouble with this topic too Qt Architecture Advice Needed

4

1 回答 1

2

每个 -> 运算符都是一个索引间接,它会花费一两个周期,具体取决于处理器,如果它的管道足够好,它可能是不可见的。

然而,这里真正的问题是“与什么相比?” 您正在考虑使用哪些其他实现技术来解决此问题?除非您有可行的替代方案,否则您的问题真的毫无意义。

同样,关于虚函数和非虚函数的相对效率的常见问题是没有意义的,除非它考虑到如何以两种方式获得相同的效果。在非虚拟情况下,这至少相当于一个“if”或“switch”,其成本必须添加到比较中。

于 2013-09-17T00:11:03.077 回答