访问成员变量与局部变量的C ++性能 [英] C++ performance of accessing member variables versus local variables

查看:88
本文介绍了访问成员变量与局部变量的C ++性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个类访问成员变量或局部变量是否更有效?例如,假设您有一个(回调)方法,其唯一职责是接收数据,对其进行计算,然后将其传递给其他类。从性能角度来看,拥有该方法在接收数据时填充的成员变量列表是否更有意义?还是每次调用回调方法时都声明局部变量?

Is it more efficient for a class to access member variables or local variables? For example, suppose you have a (callback) method whose sole responsibility is to receive data, perform calculations on it, then pass it off to other classes. Performance-wise, would it make more sense to have a list of member variables that the method populates as it receives data? Or just declare local variables each time the callback method is called?

假设此方法每秒被调用数百次...

Assume this method would be called hundreds of times a second...

如果我不清楚,这里有一些简单的例子:

In case I'm not being clear, here's some quick examples:

// use local variables
class thisClass {
    public:
        void callback( msg& msg )
        {
            int varA;
            double varB;
            std::string varC;
            varA = msg.getInt();
            varB = msg.getDouble();
            varC = msg.getString();

            // do a bunch of calculations
         }

};

// use member variables
class thisClass {
    public:
        void callback( msg& msg )
        {
             m_varA = msg.getInt();
             m_varB = msg.getDouble();
             m_varC = msg.getString();

             // do a bunch of calculations
        }

    private:
        int m_varA;
        double m_varB;
        std::string m_varC;

};


推荐答案

执行摘要:在几乎所有情况下,它都不会没关系,但是局部变量有一点优势。

Executive summary: In virtually all scenarios, it doesn't matter, but there is a slight advantage for local variables.

警告:您正在进行微优化。您将花费大量时间尝试理解应该赢得一纳秒的代码。

Warning: You are micro-optimizing. You will end up spending hours trying to understand code that is supposed to win a nanosecond.

警告:在您的情况下,性能不是问题,而是角色变量-是临时变量还是thisClass的状态?

Warning: In your scenario, performance shouldn't be the question, but the role of the variables - are they temporary, or state of thisClass?

警告:优化的第一,第二和最后一条规则:测量!

Warning: First, second and last rule of optimization: measure!

首先,请查看为x86生成的典型程序集(您的平台可能有所不同):

First of all, look at the typical assembly generated for x86 (your platform may vary):

// stack variable: load into eax
mov eax, [esp+10]

// member variable: load into eax
mov ecx, [adress of object]
mov eax, [ecx+4]

一次对象的地址被加载,并在一个寄存器中,指令是相同的。加载对象地址通常可以与较早的指令配对,并且不会影响执行时间。

Once the address of the object is loaded, int a register, the instructions are identical. Loading the object address can usually be paired with an earlier instruction and doesn't hit execution time.

但这意味着ecx寄存器不可用于其他优化。 但是,现代CPU进行了一些棘手的欺骗,以减少问题的发生。

But this means the ecx register isn't available for other optimizations. However, modern CPUs do some intense trickery to make that less of an issue.

此外,访问许多对象时,这可能会额外花费您。 但是,这少于一个周期的平均值,并且配对指令通常还有更多的机会。

Also, when accessing many objects this may cost you extra. However, this is less than one cycle average, and there are often more opprtunities for pairing instructions.

内存位置:这是一个机会赢得大时间的筹码。堆栈的顶部实际上总是在L1高速缓存中,因此加载需要一个周期。该对象更有可能被推回二级缓存(经验法则,10个周期)或主存储器(100个周期)。

Memory locality: here's a chance for the stack to win big time. Top of stack is virtually always in the L1 cache, so the load takes one cycle. The object is more likely to be pushed back to L2 cache (rule of thumb, 10 cycles) or main memory (100 cycles).

但是,您只需为首次访问付费。如果您只有一个访问权限,那么10或100个周期就不会引起注意。如果您有成千上万次访问,则对象数据也将存储在L1缓存中。

However, you pay this only for the first access. if all you have is a single access, the 10 or 100 cycles are unnoticable. if you have thousands of accesses, the object data will be in L1 cache, too.

总而言之,增益是如此之小,以至于复制成员几乎毫无意义。变量转化为本地变量以获得更好的性能。

In summary, the gain is so small that it virtually never makes sense to copy member variables into locals to achieve better performance.

这篇关于访问成员变量与局部变量的C ++性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆