为什么局部变量访问比Python中的类成员访问快? [英] Why is local variable access faster than class member access in Python?

查看:114
本文介绍了为什么局部变量访问比Python中的类成员访问快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试解决一个更复杂的问题时,我来比较本地变量和成员变量的访问速度.

While trying to tackle a more complex problem, I came to compare access speed to local variable vs member variables.

这里有一个测试程序:

#!/usr/bin/env python

MAX=40000000

class StressTestMember(object):
    def __init__(self):
        self.m = 0

    def do_work(self):
        self.m += 1
        self.m *= 2

class StressTestLocal(object):
    def __init__(self):
        pass

    def do_work(self):
        m = 0
        m += 1
        m *= 2

# LOCAL access test
for i in range(MAX):
    StressTestLocal().do_work()

# MEMBER access test
for i in range(MAX):
    StressTestMember().do_work()

我知道在每次迭代中实例化StressTestMemberStressTestLocal似乎是个坏主意,但是在建模程序中,它们基本上是活动记录,这是有意义的.

I know it might look like a bad idea to instantiate StressTestMember and StressTestLocal on each iterations but it makes sense in the modeled program where these are basically Active Records.

经过简单的基准测试

  • 本地访问测试:0m22.836
  • 会员访问测试:0分32.648秒

本地版本的速度提高了约33%,但仍属于课程的一部分.为什么?

The local version is ~33% faster while still part of a class. Why?

推荐答案

self.m += 1意味着您必须查找名为self的局部变量,然后找到名为m

self.m += 1 means you have to look up a local variable called self and then find the attribute called m

当然,如果您只需要查找局部变量,则无需执行额外的步骤就会更快.

Of course if you just have to look up a local variable, it will be faster without the extra step.

看看幕后发生的事情可能是有用的:

It can be useful to look at what is happening under the hood:

>>> import dis
>>> dis.dis(StressTestLocal.do_work)
 18           0 LOAD_CONST               1 (0)
              3 STORE_FAST               1 (m)

 19           6 LOAD_FAST                1 (m)
              9 LOAD_CONST               2 (1)
             12 INPLACE_ADD         
             13 STORE_FAST               1 (m)

 20          16 LOAD_FAST                1 (m)
             19 LOAD_CONST               3 (2)
             22 INPLACE_MULTIPLY    
             23 STORE_FAST               1 (m)
             26 LOAD_CONST               0 (None)
             29 RETURN_VALUE        
>>> dis.dis(StressTestMember.do_work)
 10           0 LOAD_FAST                0 (self)
              3 DUP_TOP             
              4 LOAD_ATTR                0 (m)
              7 LOAD_CONST               1 (1)
             10 INPLACE_ADD         
             11 ROT_TWO             
             12 STORE_ATTR               0 (m)

 11          15 LOAD_FAST                0 (self)
             18 DUP_TOP             
             19 LOAD_ATTR                0 (m)
             22 LOAD_CONST               2 (2)
             25 INPLACE_MULTIPLY    
             26 ROT_TWO             
             27 STORE_ATTR               0 (m)
             30 LOAD_CONST               0 (None)
             33 RETURN_VALUE        

这篇关于为什么局部变量访问比Python中的类成员访问快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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