@ Is faster than dot

Multiplication by @, which is available in python3.5 and later.

Benchmark:

use_at


import numpy as np

N=1000
LOOP=10
a=np.arange(N*N).reshape(N,N)

for _ in range(LOOP):
  b=a@a@a

use_dot


import numpy as np

N=1000
LOOP=10
a=np.arange(N*N).reshape(N,N)

for _ in range(LOOP):
  b=a.dot(a).dot(a)
How to write time
use_at user 0m12.344s
use_dot user 0m35.812s

How, @ is about three times faster. I think it depends on the size of the matrix, the type of the value, the processing system, etc., but if you execute the above code at hand, you will get this result.

I was a little surprised.

Recommended Posts

@ Is faster than dot
sympy.Mul is much faster than sympy.prod
[Small story] In Python, i = i + 1 is slightly faster than i + = 1.
Python release cycle is faster!
Golang vs. Python – Is Golang Better Than Python?
[Python competition professional memo] LRU cache is faster than programmatic memoization
Which is faster, Python shuffle or sample?