Initialise Cython Memoryview Efficiently
Solution 1:
See this answer for a comparison of different ways of allocating memory. If your needs are simple (just indexing) pay particular attention to 'cpython.array raw C type', you can create a cpython array for fast creation then use as_ints[i]
for fast unsafe indexing, or if you really do need a memory view, the memory view on a cpython array is 3x faster than a numpy array.
Without having a bigger picture of what your code does it's difficult to offer more specific advice. For example if possible you would be better served using a two-dimensional array as it tends to be much more efficient to allocate one large chunk of memory than lots of little ones, and for instance it's much quicker to make lots of little memory view slices of one big memory view with a big hunk of allocated memory, than to create a bunch of little memory views each with their own little piece of allocated memory.
Solution 2:
Your memoryview is slow(er than strictly necessary) because Python needs to reference-count it. You can allocate the memory by hand using the Python/C API, but then you're responsible for freeing it when you no longer need it.
Don't do this unless you've used a profiler and are seeing an unacceptable amount of refcounting overhead. Premature optimization is never a good idea, and it's easy to introduce memory leaks or segfaults with this method.
Post a Comment for "Initialise Cython Memoryview Efficiently"