1

I saw someone trying to use python and numpy to do simulations. However, it was quiet slow. I read his code and it turnes out to me that he only used numpy packages when dealing with the array/matrix calculation, and all the other variables and codes was defined in the python main console(i.e. for loops).

My questions are:

  1. Is it necessary to write all the variable as numpy variable, for example np.float in order to get the full capability of numpy package?

  2. Also, as from what I'm understanding, python's for loop is quiet slow. But I don't want to use cython for the sake of simplicity and readibility. It there a way for the numpy to use some special iteration process to accelerate the calculation? I saw a page that looks like it but did not quite understand(https://docs.scipy.org/doc/numpy/reference/arrays.nditer.html). Bascially, how to utilize the full capability ofnumpy package?

J C
  • 181
  • 7
  • 1
    `numpy` defines a `ndarray` class, multidimensional arrays. Indexing, slicing, and calculations are performed in fast compiled code. Iterations, including ones using `nditer` are slow, slower even then Python code using lists. I'd suggest browsing some of the other SO questions with this tag to get an idea of the tasks it's being used for. Otherwise I think your question is too vague for SO. – hpaulj Sep 07 '18 at 06:48
  • 1
    It's not good `numpy` style to create variables like `x = np.float64(1.232)`. Most `numpy` functions and methods work with Python scalars and lists. Where needed `numpy` code turns inputs into arrays: `x = np.asarray(x)` etc. – hpaulj Sep 07 '18 at 07:09
  • It would all be so much easier if you had some code to comment upon. – norok2 Sep 07 '18 at 09:25

1 Answers1

1

Since you didn't provide details of your program, I'll assume you are wondering if numpy.ndarray is faster than using normal python loop.

Yes, it IS much faster.

It's called vectorization. It's faster because:

  1. It'll be executed by the precompiled C/C++ library and thus faster than executing python script
  2. It takes advantage of the architecture of modern CPU. See: link

That said, it also depends on what you want to do with it. If you are performing math/matrix operations, than vectorization also provides better readability (since it just looks similar to the equation) and more boost since the operations are mostly numerical. If the operations are not numerical, than you'll get less boost and thus can do it the python way.

James Bear
  • 434
  • 2
  • 4