python - NumPy vs Cython - nested loop so slow? -


How I am confused how NumPy for 3D array is much slower than nested loop Cython I wrote trivial example. Was there. Import

Python / MPI version:

as np def my_func (a, b, c): z = xrange for z (401): xrange (401) for x: xrange (401) for x: if a [z, y, x] == 0 and b [x, y, z] & gt; = 0: c [z, y, x] = 1b [z, y, x] = z * y * xs + = 1 return b = np.zeros ((401,401,401), dtype = np.float32) b = np .zeros ((401,401,401), dtype = Np.uint32) c = np.zeros ((401,401,401), dtype = np.uint8) s = my_func (a, b, c)

Sithonized version:

  NP Simon Sethon @ cython.boundscheck as Simply P.P. (wrong) @ cython.wraparound (False) def my_func (np.float32_t [:,:, :: 1] A, np.uint32_t [:,:,:: 1] b, np.uint8_t [:,:,, :: 1] c): border for CDF np.uint16_t z, y, x cdef np.uint32_t s = 0 In range (401): For range y (401): For the range in range (401): If a [z, y, x] == 0 and b [x, y, z] & gt; = 0: c [z, y, x] = 1b [z, y, x] = z * y * xs = s + 1 return s  

my_func () The synthesized version of runs roughly 6500x faster weekly function only if-statement and array access can be 10000x faster. The Python version of my_func () takes 500.651 seconds to finish. Is it getting shorter than a relatively small 3D array or did I make a mistake in the code?

Seether version 0.21.1, Python 2.7.5, GCC 4.8.1, Xbuntu 13.10.

Python is an interpreted language, one of the advantages of compiling machine code is that you get big speed It's happening, especially with things like Nested Loop.

I do not know what your expectations are, but all the interpretive languages ​​will be very slow in the things that you are trying to do (though some extent help may be available).

The trick to achieve good performance from Nampee (or MATLAB or something similar) is to avoid looping altogether and instead replacing your code in some operations on the larger matrix, thus replacing the Python code Possessing (heavily customized) machine code will be in the Library.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -