On Nov 16, 2008, at 12:31 AM, Aaron DeVore wrote:

> Doesn't that give the overhead of a tuple? I would check this myself
> but I don't have access to a Cython-enabled computer at the moment. :(

Yes, it does, but it is relatively cheap. Much cheaper than the  
overhead of a dictionary lookup for instance. And also much less  
painful then manually reference counting using PyDict_Next.

> On Sat, Nov 15, 2008 at 11:40 PM, Robert Bradshaw
> <[EMAIL PROTECTED]> wrote:
>> On Nov 15, 2008, at 11:28 PM, Aaron DeVore wrote:
>>
>>> So would this be better?
>>>
>>> for k in d:
>>>     v = d[k]
>>>     # Do stuff with k and v
>>
>> No, the
>>
>> for k, v in d.items():
>>     ...
>>
>> is best.
>>
>> - Robert
>>
>>
>>
>>>
>>> On Sat, Nov 15, 2008 at 1:53 PM, Robert Bradshaw
>>> <[EMAIL PROTECTED]> wrote:
>>>> On Nov 15, 2008, at 1:17 PM, Aaron DeVore wrote:
>>>>
>>>>> I'm trying to use PyDict_Next to iterate over a dict in a way
>>>>> that is
>>>>> identical to this statement:
>>>>> for k, v in d.items():
>>>>>     # do stuff with key and value
>>>>>
>>>>> PyDict_Next has the signature:
>>>>> int PyDict_Next(PyObject *dictionary, Py_ssize_t *pos, PyObject
>>>>> **key,
>>>>> PyObject **value)
>>>>>
>>>>>
>>>>> The basic idea is that the dict uses pos to track which key it is
>>>>> on.
>>>>> The key is then assigned to the key pointer and the corresponding
>>>>> value is assigned to the value pointer. It can really help
>>>>> efficiency
>>>>> because it doesn't involve iterators, tuples, etc.
>>>>>
>>>>> The most obvious code is the following:
>>>>>
>>>>> cdef int pos = 0
>>>>> cdef object key, value
>>>>> while PyDict_Next(d, &pos, &key, &value):
>>>>>     # do stuff with key and value
>>>>>
>>>>> However, apparently &python_object is not legal and I'm running  
>>>>> into
>>>>> odd type issues with &pos. Is there a way to get around those
>>>>> limitations?
>>>>
>>>> Yes, declare them to be PyObject* rather than object. Then you'll
>>>> have to do all refcounting manually (as you would have had to do
>>>> anyways, as PyDict_Next doesn't decref its input). However, I doubt
>>>> iterating over the dict manually like that will be a significant
>>>> speed increase than the basic Python way of doing it.
>>>>
>>>> - Robert
>>>>
>>>> _______________________________________________
>>>> Cython-dev mailing list
>>>> [email protected]
>>>> http://codespeak.net/mailman/listinfo/cython-dev
>>>>
>>> _______________________________________________
>>> Cython-dev mailing list
>>> [email protected]
>>> http://codespeak.net/mailman/listinfo/cython-dev
>>
>> _______________________________________________
>> Cython-dev mailing list
>> [email protected]
>> http://codespeak.net/mailman/listinfo/cython-dev
>>
> _______________________________________________
> Cython-dev mailing list
> [email protected]
> http://codespeak.net/mailman/listinfo/cython-dev

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to