Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: python 3 problem: how to convert an extension method into a classMethod

Reply
Thread Tools

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

 
 
Peter Otten
Guest
Posts: n/a
 
      02-27-2013
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
python 3 problem: how to convert an extension method into a classMethod Robin Becker Python 1 02-27-2013 04:46 AM
Re: python 3 problem: how to convert an extension method into a classMethod Peter Otten Python 0 02-26-2013 08:26 PM
Re: python 3 problem: how to convert an extension method into a classMethod Mark Lawrence Python 0 02-26-2013 07:33 PM
Re: python 3 problem: how to convert an extension method into a classMethod Peter Otten Python 0 02-26-2013 06:38 PM
Re: python 3 problem: how to convert an extension method into a classMethod Dave Angel Python 0 02-26-2013 05:52 PM



Advertisments