Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #40052

Re: python 3 problem: how to convert an extension method into a class Method

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 <python-python-list@m.gmane.org>
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; 'none,': 0.05; 'interpreted': 0.07; 'python': 0.09; 'function:': 0.09; 'indication': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:into': 0.09; 'subject:method': 0.09; 'utf8': 0.09; 'def': 0.10; 'subject:python': 0.11; 'extension': 0.13; "'from": 0.16; 'a(object):': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'subject:class': 0.16; 'wrote:': 0.17; 'bytes': 0.17; 'instance': 0.17; 'refers': 0.17; 'unicode': 0.17; '>>>': 0.18; 'math': 0.20; 'equivalent': 0.20; 'trying': 0.21; 'import': 0.21; 'subject:problem': 0.22; 'example': 0.23; 'class.': 0.23; 'pass': 0.25; 'header:User- Agent:1': 0.26; 'implemented': 0.27; 'header:X-Complaints-To:1': 0.28; 'actual': 0.28; 'run': 0.28; '0.5': 0.29; 'coded': 0.29; 'measure': 0.29; 'overhead': 0.29; 'wrap': 0.29; 'class': 0.29; "i'm": 0.29; 'function': 0.30; 'expect': 0.31; 'code': 0.31; '(and': 0.32; '(2)': 0.32; 'to:addr:python-list': 0.33; '(1)': 0.34; 'similar': 0.35; 'received:org': 0.36; 'but': 0.36; 'wanted': 0.36; 'compare': 0.36; 'method': 0.36; 'should': 0.36; 'turn': 0.36; 'two': 0.37; 'being': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'takes': 0.39; 'called': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'your': 0.60; 'between': 0.63; 'here': 0.65; 'therefore': 0.65; 'results': 0.65; 'analysis': 0.70; 'otten': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: python 3 problem: how to convert an extension method into a class Method
Date Wed, 27 Feb 2013 12:14 +0100
Organization None
References <512CEF0C.3020906@chamonix.reportlab.co.uk> <kgivf6$ojl$1@ger.gmane.org> <512DE1AB.7000100@chamonix.reportlab.co.uk>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084be1f.dip.t-dialin.net
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2608.1361963645.2939.python-list@python.org> (permalink)
Lines 69
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1361963645 news.xs4all.nl 6900 [2001:888:2000:d::a6]:53297
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:40052

Show key headers only | View raw


Robin Becker wrote:

> On 26/02/2013 18:38, Peter Otten wrote:
>> Robin Becker wrote:
> ...........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
>>
>>
> this analysis might be relevant if I wanted to use sqrt. However, in my
> case the method takes
> 
> 
> 
>                        py  C
> utf8 bytes            50  20 usec
> unicode               39  15
> 
> here py refers to a native python method and C  to the extension method
> after adding to the class. Both are called via an instance of the class.

I think you misunderstood. You compare the time it takes to run the function 
coded in C and its Python equivalent -- that difference is indeed 
significant.

But what I was trying to measure was the difference between two ways to wrap 
the C function:

Given a function cfunc implemented in C and the two ways of turning it into 
a method

(1)
class A(object):
   pass

def method(self, ...):
   return cfunc(self, ...)
A.method = method

(2)
class A(object):
   pass

A.method = new.instancemethod(cfunc, None, A)

I interpreted my timeit results as an indication that both ways have roughly 
the same overhead (method (1) being 0.027 usec faster).

I don't have your code available, that's why I picked math.sqrt as an 
example for cfunc. 

I expect that you will get a similar result with your actual cfunc and 
therefore can (and should IMO) use method (1) in both Python 2 and 3
-- but of course not being able to measure it myself it may turn out I'm 
wrong.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: python 3 problem: how to convert an extension method into a class Method Peter Otten <__peter__@web.de> - 2013-02-27 12:14 +0100

csiph-web