优化覆盖/溺爱(覆盖__getattribute __ / __ getattr__)

时间:2016-03-24 07:13:20

标签: python python-2.7

我当前的代码使用了覆盖__getattribute __ / __ getattr__的常用类。分析表明在重写的函数中花费了大量的时间。我强烈简化了用于测量纯覆盖性能影响的代码,如下所示:

from timeit import default_timer

class Test(object):
    def __init__(self):
        self.a = 1.0

class Test1(object):
    def __init__(self):
        self.a = 1.0

    def __getattribute__(self, item):
        return object.__getattribute__(self, item)

class Test2(object):
    def __init__(self):
        self.a = 1.0

    def __getattribute__(self, item):
        return super(Test2, self).__getattribute__(item)

class ObjectWrapper(object):
    def __init__(self, ocls, obj=None):
        self.__ocls = ocls
        self.__obj = None
        if obj is None or isinstance(obj, ocls):
            self.__obj = obj
        else:
            raise RuntimeError(''.join(str(x) for x in ("The value is ", type(obj), \
                ", but it must be None or instance of ", self.__ocls.__name__)))

    def set_inner_object(self, obj):
        if obj is None or isinstance(obj, self.__ocls):
            self.__obj = obj
        else:
            raise RuntimeError(''.join(str(x) for x in ("The value is ", type(obj), \
                ", but it must be None or instance of ", self.__ocls.__name__)))

    def __getattr__(self, name):
        return getattr(self.__obj, name)

def main():
    x = Test()
    x1 = Test1()
    x2 = Test2()
    xw = ObjectWrapper(Test, x)

    accsum = 0.0
    accsum1 = 0.0
    accsum2 = 0.0
    accsumw = 0.0

    s = default_timer()
    for i in xrange(0, 100000000):
        accsum += x.a
    e = default_timer()

    s1 = default_timer()
    for i in xrange(0, 100000000):
        accsum1 += x1.a
    e1 = default_timer()

    s2 = default_timer()
    for i in xrange(0, 100000000):
        accsum2 += x2.a
    e2 = default_timer()

    sw = default_timer()
    for i in xrange(0, 100000000):
        accsumw += xw.a
    ew = default_timer()

    print "speed factor Test1/Test: ", (e1 - s1) / (e - s)
    print "speed factor Test2/Test: ", (e2 - s2) / (e - s)
    print "speed factor Test wrapped/Test: ", (ew - sw) / (e - s)

if __name__ == '__main__':
    main()

压倒一切的影响是巨大的。这是输出:

speed factor Test1/Test:  6.32820892871
speed factor Test2/Test:  8.4176175507
speed factor Test wrapped/Test:  11.6202852701

Python是否正常(python不是我的母语编程语言),为什么? 这是压倒一切的价格和/和溺爱的价格吗?!

如何改善表现?

1 个答案:

答案 0 :(得分:0)

是的,你使用的抽象越多,事情就越慢。主要的Python实现使用散列表来处理所有内容,而额外的间接层通常意味着额外的散列表查找。

以下是您可以咀嚼的一些反汇编:

>>> dis.dis(Test2.__getattribute__)
  5           0 LOAD_GLOBAL              0 (super)
              3 LOAD_GLOBAL              1 (Test2)
              6 LOAD_FAST                0 (self)
              9 CALL_FUNCTION            2 (2 positional, 0 keyword pair)
             12 LOAD_ATTR                2 (__getattribute__)
             15 LOAD_FAST                1 (item)
             18 CALL_FUNCTION            1 (1 positional, 0 keyword pair)
             21 RETURN_VALUE

请注意,对于每个LOAD_GLOBAL,您基本上都在进行哈希表查找,然后是LOAD_ATTR的另一个哈希表查找(我认为)。你正在做更多工作所以需要花费更多时间。除此之外,还要考虑执行您不需要的功能的成本。