r/learnpython • • 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

12 comments sorted by

View all comments

Show parent comment

1

u/sausix Sep 13 '26

I like Math a bit but not in full detail. I'm just aware of the one method of calculating the Mandelbrot set.

Found my app. This was the core method of calculating and tracking the iterations probably still optimizable:

for i in range(self.iterations):
    Z[active] = Z[active] ** self.exp + C[active]
    active[np.abs(Z) > 2] = False
    Ni[active] += 1
    Ni = (Ni / self.iterations * 255).astype('uint8')
    color = cv2.applyColorMap(Ni, cv2.COLORMAP_RAINBOW)

1

u/BluishMontoya Sep 13 '26

The thing is, floating point numbers give out after a certain level of zoom because the difference between the coordinates of adjacent pixels is too small to represent. Perturbation theory is a way to be able to still calculate higher levels of zoom with floating point and a smaller number of high precision calculations, but the math gets way more complicated than simple mandelbrot rendering.