メモリビューを使用して cython に標準のクイックソートを実装しようとしています。これが私のコードです:
def quicksort_cython(double[:] l):
_quicksort(l, 0, len(l) - 1)
cdef void _quicksort(double[:] l, double start, double stop):
cdef double pivot, left, right, tmp
if stop - start > 0:
pivot = l[start]
left = start
right = stop
while left <= right:
while l[left] < pivot:
left += 1
while l[right] > pivot:
right -= 1
if left <= right:
tmp = l[left]
l[left] = l[right]
l[right] = tmp
left += 1
right -= 1
_quicksort(l, start, right)
_quicksort(l, left, stop)
ただし、標準setup.py
ファイルとpython setup.py build_ext --inplace
コマンドを使用したコンパイル中に、メモリビュー アクセスに関する複数のエラーが発生します。
Error compiling Cython file:
------------------------------------------------------------
...
cdef void _quicksort(double[:] l, double start, double stop):
cdef double pivot, left, right, tmp
if stop - start > 0:
pivot = l[start]
^
------------------------------------------------------------
quicksort_cython_opt3.pyx:9:23: Invalid index for memoryview specified
誰かが私が間違っていることを教えてもらえますか? また、私はCythonが初めてなので、パフォーマンスを改善するためのヒントをいただければ幸いです..ありがとう!