python – 优化覆盖/溺爱(覆盖__getattribute __ / __ getattr__
发布时间:2020-12-20 13:11:00 所属栏目:Python 来源:网络整理
导读:我当前的代码使用__getattribute __ / __ getattr__覆盖常用的类.分析表明在重写的函数中花费了大量的时间.我强烈简化了用于测量纯覆盖性能影响的代码,如下所示: from timeit import default_timerclass Test(object): def __init__(self): self.a = 1.0cla
我当前的代码使用__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 ",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不是我的母语编程语言),为什么? 如何提高性能? 解决方法
是的,你使用的抽象越多,事情就越慢.主要的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(我认为).你正在做更多的工作,所以需要更多的时间.除此之外,还要增加执行您不需要的功能的成本. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |