harry lakins - 5 days ago 5

Python Question

When you have two numPy matrix, you could call a dot function to multiply them. Or you could loop through each manually and multiply each value manually. Why and what is the speed difference ? Surely dot function still has to do that but Lower level?

Answer

Yes, both approaches will have to loop over the values in the two matrices. However, python is dynamically typed such that the body of the loop needs to check the types of the three indices used for iteration, ensure that indexing the two input matrices is supported, determine the type of the values extracted from the matrices, ...

The numpy implementation is, as you said, lower-level and makes stronger assumptions about the input and output. In particular, the matrix multiplication is implemented in a statically typed language (C or Fortran--I can't quite remember) such that the overhead of type checking disappears. Furthermore, indexing in lower-level languages is a relatively simple operation.