简体   繁体   中英

Fastest way to compute matrix dot product

I compute the dot product as follows:

import numpy as np
A = np.random.randn(80000, 3000)
B = np.random.randn(3000, 50)
C = np.dot(A, B)

Running this script takes about 9 seconds:

Mac@MacBook-Pro:~/python_dot_product$ time python dot.py 

real    0m9.042s
user    0m10.927s
sys     0m0.911s

Could I do any better? Does numpy already use the ideal balance for the cores?

The last two answers at this SO answer should be helpful.

The last one pointed me to SciPy documentation , which includes this quote:

"[np.dot(A,B) is evaluated using BLAS, which] will normally be a library carefully tuned to run as fast as possible on your hardware by taking advantage of cache memory and assembler implementation. But many architectures now have a BLAS that also takes advantage of a multicore machine. If your numpy/scipy is compiled using one of these, then dot() will be computed in parallel (if this is faster) without you doing anything."

So it sounds like it depends on your specific hardware and SciPy compilation. Sometimes np.dot(A,B) will utilize your multiple cores/processors, sometimes it might not.

To find out which case is yours, I suggest running your toy example (with larger matrices) while you have your system monitor open, so you can see whether just one CPU spikes in activity, or if multiple ones do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM