Why are numpy and torch so much faster than R for matrix multiplication?

Viewed 416

In Python:

>>> import numpy as np
>>> x0=np.random.rand(3000,3000)
>>> t=time.perf_counter(); y0=np.matmul(x0,x0); time.perf_counter()-t
0.8358144999947399

>>> import torch
>>> x=torch.rand(3000,3000)
>>> t=time.perf_counter(); y0=np.matmul(x,x); time.perf_counter()-t
0.4304323000833392

In R:

> a=matrix(runif(9000000), 3000, 3000)
> a1=a%*%a
> system.time({a1=a%*%a})
   user  system elapsed 
  16.53    0.04   16.57

Why is 20-fold difference with numpy and 40-fold difference with torch?

0 Answers
Related