r/learnpython • u/BluishMontoya • Sep 13 '26
Cython memoryviews?
I'm using a function I've compiled with cython, and I have to pass it a few very long lists. I'm pretty sure I should be using memoryviews, but do I convert the lists to memoryviews in the python part of my program or the cython part? It would be way better if I could do it in the python part because I'm passing the same lists to the function a few million times.
4
Upvotes
View all comments
Show parent comment
1
u/futura-bold Sep 13 '26
Well, you can use numpy alone to calculate the Mandelbrot Set. The numpy floating-point array calculation is very fast and makes use of the CPU's SIMD instructions to calculate chunks of the array in parallel. The downside is that simplest array-implementation of the Mandelbrot Set algorithm would require that the same fixed number of iterations would be required for each pixel, regardless of the number of iterations required to reach the "escape" value for a given pixel, and that a mask would be required to ignore those unnecessary calculations past the escape values. Nevertheless, the following example should only take one second to run:
I've tried a more elaborate method to avoid those unnecessary calculations, but it starts getting complicated. See here:
https://www.reddit.com/r/PythonLearning/comments/1vsv196