Why is matrix multiplication faster with numpy than with ctypes in Python?

后端 未结 6 1890
醉话见心
醉话见心 2020-11-29 00:11

I was trying to figure out the fastest way to do matrix multiplication and tried 3 different ways:

  • Pure python implementation: no surprises here.
  • Nump
6条回答
  •  既然无缘
    2020-11-29 00:53

    NumPy uses a highly-optimized, carefully-tuned BLAS method for matrix multiplication (see also: ATLAS). The specific function in this case is GEMM (for generic matrix multiplication). You can look up the original by searching for dgemm.f (it's in Netlib).

    The optimization, by the way, goes beyond compiler optimizations. Above, Philip mentioned Coppersmith–Winograd. If I remember correctly, this is the algorithm which is used for most cases of matrix multiplication in ATLAS (though a commenter notes it could be Strassen's algorithm).

    In other words, your matmult algorithm is the trivial implementation. There are faster ways to do the same thing.

提交回复
热议问题