
Hi Pypies, I'm new to the list, so let me start by saying I totally love pypy and am using it for my bioinformatics codes. I realise this isn't exactly a pypy specific problem, by the standard array module doesn't provide sort() on arrays. In consequence you either have to convert them to lists and use list.sort() or you have to implement sort on them yourself. I've implemented several sorts (radix, american flag, quick sort, in-place heap sort) and they all perform slower than converting to a list and sorting that. It strikes me that any python implementation should benefit from an array.sort() method since unboxing/type checking is unnecessary. What is the wisdom of this community on the matter? It's something I'd consider taking on if it was generally deemed worthwhile. Tom. -- Thomas Conway drtomc@gmail.com My friends, love is better than anger. Hope is better than fear. Optimism is better than despair. So let us be loving, hopeful and optimistic. And we'll change the world. - Jack Layton

Hi Thomas, On 26 November 2016 at 03:32, Thomas Conway <drtomc@gmail.com> wrote:
I realise this isn't exactly a pypy specific problem, by the standard array module doesn't provide sort() on arrays.
I fear it's not PyPy's place to add such new methods to standard Python modules. You'd need to go to CPython and discuss the issue with them---which is not really a fun process imho. However, here are a few paths: * First, do you really need the array module? If you use a list containing only signed, machine-sized integers, then that list is represented in PyPy as compactly as an array of the 'l' type. It won't work if you really need more compact arrays, but if you don't, then one solution to your problem is simply to use lists and not arrays in the first place. * Or, use cffi to call qsort(). This is simpler (and most probably faster) than writing the sort in pure Python. It works with an array object from the array module, or anything supporting the buffer interface: ``lib.qsort(ffi.from_buffer(my_array), more_args...)``. You'd need to use the ``set_source()`` mode of CFFI in order to write an efficient comparison function in C to pass as argument. * (Additionally, you can use cffi's ``ffi.new("int[]", length)`` in the first place instead of the array module, as it is similar but more flexible: see http://cffi.readthedocs.io/en/latest/overview.html#struct-array-example-mini... . This does not really change anything though.) * The above seem like more general solutions than adding a sort method to the array module. However, if you really want to do that, then maybe create a function ``__pypy__.sort_buffer()`` instead, able to sort a buffer of integers of some size; it would not be on the array module specifically, and clearly it would be a pypy extension. A bientôt, Armin.

Hi Thomas, On 26 November 2016 at 03:32, Thomas Conway <drtomc@gmail.com> wrote:
I realise this isn't exactly a pypy specific problem, by the standard array module doesn't provide sort() on arrays.
I fear it's not PyPy's place to add such new methods to standard Python modules. You'd need to go to CPython and discuss the issue with them---which is not really a fun process imho. However, here are a few paths: * First, do you really need the array module? If you use a list containing only signed, machine-sized integers, then that list is represented in PyPy as compactly as an array of the 'l' type. It won't work if you really need more compact arrays, but if you don't, then one solution to your problem is simply to use lists and not arrays in the first place. * Or, use cffi to call qsort(). This is simpler (and most probably faster) than writing the sort in pure Python. It works with an array object from the array module, or anything supporting the buffer interface: ``lib.qsort(ffi.from_buffer(my_array), more_args...)``. You'd need to use the ``set_source()`` mode of CFFI in order to write an efficient comparison function in C to pass as argument. * (Additionally, you can use cffi's ``ffi.new("int[]", length)`` in the first place instead of the array module, as it is similar but more flexible: see http://cffi.readthedocs.io/en/latest/overview.html#struct-array-example-mini... . This does not really change anything though.) * The above seem like more general solutions than adding a sort method to the array module. However, if you really want to do that, then maybe create a function ``__pypy__.sort_buffer()`` instead, able to sort a buffer of integers of some size; it would not be on the array module specifically, and clearly it would be a pypy extension. A bientôt, Armin.
participants (3)
-
Armin Rigo
-
Konstantin Lopuhin
-
Thomas Conway