Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'win32': 0.03; 'heavily': 0.04; 'importerror:': 0.05; 'none,': 0.05; 'except:': 0.07; 'layers': 0.07; 'try:': 0.07; 'python': 0.09; 'happen?': 0.09; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sep': 0.09; 'subject:into': 0.09; 'subject:method': 0.09; 'def': 0.10; 'subject:python': 0.11; 'extension': 0.13; "'from": 0.16; '(another': 0.16; 'bind': 0.16; 'measurement': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject: \n ': 0.16; 'subject:class': 0.16; 'unbound': 0.16; 'wrote:': 0.17; 'instance': 0.17; '>>>': 0.18; 'module': 0.19; 'math': 0.20; 'bit': 0.21; 'import': 0.21; '"",': 0.22; 'subject:problem': 0.22; 'work.': 0.23; 'random': 0.24; 'external': 0.24; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '(most': 0.27; "doesn't": 0.28; 'header:X-Complaints-To:1': 0.28; 'skip:( 20': 0.28; 'all.': 0.28; 'fine': 0.28; '0.5': 0.29; 'clever': 0.29; 'methods.': 0.29; 'overhead': 0.29; 'class': 0.29; 'stuff': 0.30; 'file': 0.32; 'from:addr:yahoo.co.uk': 0.32; 'traceback': 0.33; 'to:addr:python- list': 0.33; 'there': 0.35; 'received:org': 0.36; 'but': 0.36; 'method': 0.36; 'too': 0.36; 'subject:: ': 0.38; 'mark': 0.38; 'fact': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'called': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'times': 0.63; 'more': 0.63; 'great': 0.64; 'our': 0.65; 'skip:n 30': 0.69; '.....': 0.75; "'new'": 0.84; 'forward.': 0.84; 'otten': 0.84; 'victory': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: python 3 problem: how to convert an extension method into a class Method Date: Tue, 26 Feb 2013 19:33:32 +0000 References: <512CEF0C.3020906@chamonix.reportlab.co.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-92-18-29-221.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.0; rv:17.0) Gecko/20130215 Thunderbird/17.0.3 In-Reply-To: X-Antivirus: avast! (VPS 130226-0, 26/02/2013), Outbound message X-Antivirus-Status: Clean X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 70 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361907166 news.xs4all.nl 6867 [2001:888:2000:d::a6]:42813 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39997 On 26/02/2013 18:38, Peter Otten wrote: > Robin Becker wrote: > >> In python 2 I was able to improve speed of reportlab using a C extension >> to optimize some heavily used methods. >> >> so I was able to do this >> >> >> class A: >> ..... >> def method(self,...): >> .... >> >> >> try: >> from extension import c_method >> import new >> A.method = new.instancemethod(c_method,None,A) >> except: >> pass >> >> and if the try succeeds our method is bound as a class method ie is >> unbound and works fine when I call it. >> >> In python 3 this doesn't seem to work at all. In fact the new module is >> gone. The types.MethodType stuff doesn't seem to work. >> >> Is there a way in Python 3.3 to make this happen? This particular method >> is short, but is called many times so adding python wrapping layers is not >> a good way forward. >> >> If the above cannot be made to work (another great victory for Python 3) >> then is there a way to bind an external method to the instance without >> incurring too much overhead. > > Hm, according to my random measurement your clever approach incurs more > overhead than the straight-forward way that continues to work in Python 3: > > $ python -m timeit -s 'from new import instancemethod >> from math import sqrt >> class A(int): pass >> A.m = instancemethod(sqrt, None, A) >> a = A(42) >> ' 'a.m()' > 1000000 loops, best of 3: 0.5 usec per loop > $ python -m timeit -s 'from math import sqrt >> class A(int): >> def m(self): >> return sqrt(self) >> a = A(42) >> ' 'a.m()' > 1000000 loops, best of 3: 0.473 usec per loop > > c:\Users\Mark\MyPython>python Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import new Traceback (most recent call last): File "", line 1, in ImportError: No module named 'new' -- Cheers. Mark Lawrence