Hi,
tav wrote:
> Whilst I'm at it, is there any advantage into turning the various
> lists into C arrays? I won't be accessing them from Python and thus
> was wondering if it would also be possible to speed up the
> __getattribute__ implementation in Namespace? Thanks!
This is what you have:
def __getattribute__(Namespace self, char *attr):
cdef int i
cdef object j, v
i = 0
for j in store[self.id]:
if j == attr:
v = self.env[i]
if hasattr(v, '__get__'):
return v.__get__(None, self)
return v
i = i + 1
raise AttributeError("'Namespace' object has no attribute %r" % attr)
If you change the "char* attr" into a plain "attr", this will speed up things
considerably. In your code, Cython has to convert "attr" to a Python string on
each loop to convert it to the Python string "j" (which is a really bad name
for a Python string, BTW).
I also don't quite understand the hasattr(v, "__get__"). Is that supposed to
access a property?
> On Sun, Nov 9, 2008 at 3:56 AM, tav <[EMAIL PROTECTED]> wrote:
>> Apologies if this is the wrong list to post to -- I couldn't find a
>> cython-users list...
I extended the list description a bit, so that it becomes clearer that this
*is* the right list.
>> I've been using Cython to speed up instantiation of a Namespace object
>> written in Python: http://paste.lisp.org/display/69989
>>
>> Fundamentally, what I am trying to do is:
>>
>> class Namespace:
>> def __init__(self, **env):
>> global id, store
>> id += 1
>> keys = store[id] = sorted(env)
>> new_env = []
>> for key in keys:
>> obj = env[key]
>> if PyFunction_Check(obj):
>> obj = staticmethod(obj)
>> new_env.append(obj)
>> self.env = new_env
>>
>> The sort() and looping (for key in keys) seems to take up most of the
>> time... how can I do this better so that it takes less time?
Why is this important? Do you create them very often, or is the size of env
the problem? We need to know what you want to optimise in order to hep you.
Note, BTW, that the speed of sort() is not influenced by Cython.
Stefan
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev