访问本地和类成员变量之间的性能差异 [英] Performance difference between accessing local and class member variables

查看:49
本文介绍了访问本地和类成员变量之间的性能差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在类成员函数中有以下代码:

I have the following code in a class member function:

int state = 0;
int code = static_cast<int>(letter_[i]);
if (isalnum(code)) {
      state = testTable[state][0];
    } else if (isspace(code)) {
        state = testTable[state][2];
    } else if (code == OPEN_TAG) {
        state = testTable[state][3];
    } else if (code == CLOSE_TAG) {
        state = testTable[state][4];
    } else {
        state = testTable[state][1];
    }

    switch (state) {
    case 1: // alphanumeric symbol was read
        buffer[j] = letter_[i];
        ++j;
        break;
    case 2: // delimeter was read
        j = 0;
    //  buffer.clear();
        break;
    }

但是,如果 state 是类成员变量而不是本地变量,则性能会显着下降(约 5 倍).我正在阅读有关访问局部变量和类成员的差异,但文本通常说它对性能影响很小.

However, if state is a class member variable rather than local, the performance drops considerably (~ 5 times). I was reading about differences in accessing local variables and class members, but texts usually say that it affects performance very slightly.

如果有帮助:我正在使用带有 -O3 选项的 MinGW GCC 编译器.

If it helps: I am using MinGW GCC compiler with -O3 option.

推荐答案

我无法重现您的观察结果,在 x86_64 上同时使用 VS10 和 g++ 进行测试.本地变体稍微快一些,可能是由于 Alan Stokes 在他的评论中所描述的,但最多约 10%.您应该检查您的时间,尝试排除任何其他问题,最好将您的所有代码简化为仍然显示此行为的非常简单的测试.

I could not reproduce your observation, testing on x86_64 with both, VS10 and g++. The local variant is slightly faster, probably due to what Alan Stokes described in his comment, but at most ~10%. You should check your timing, try to rule out any other problems and best would be the reduce all your code to a very simple test-cast which still shows this behavior.

我认为我的测试用例与您的场景非常相似,至少就像您描述的那样:

I think my test-case resembles your scenario quite good, at least like you described it:

#include <iostream>
#include <boost/timer.hpp>

const int max_iter = 1<<31;
const int start_value = 65535;

struct UseMember
{
    int member;
    void foo()
    {
        for(int i=0; i<max_iter; ++i)
        {
            if(member%2)
                member = 3*member+1;
            else
                member = member>>1;
        }
        std::cout << "Value=" << member << std::endl;
    }
};

struct UseLocal
{
    void foo()
    {
        int local = start_value;
        for(int i=0; i<max_iter; ++i)
        {
            if((local%2)!=0) /* odd */
                local = 3*local+1;
            else /* even */
                local = local>>1;
        }
        std::cout << "Value=" << local << std::endl;
    }
};


int main(int argc, char* argv[])
{
    /* First, test using member */
    std::cout << "** Member Access" << std::endl;
    {
        UseMember bar;
        bar.member = start_value;
        boost::timer T;
        bar.foo();
        double e = T.elapsed();
        std::cout << "Time taken: " << e << "s" << std::endl;
    }

    /* Then, test using local */
    std::cout << "** Local Access" << std::endl;
    {
        UseLocal bar;
        boost::timer T;
        bar.foo();
        double e = T.elapsed();
        std::cout << "Time taken: " << e << "s" << std::endl;
    }
    return 0;
}

这篇关于访问本地和类成员变量之间的性能差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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