# [SciPy-User] Element-wise multiplication performance: why operations on sliced matrices are faster?

Matthieu Brucher matthieu.brucher@gmail....
Fri Feb 10 03:45:04 CST 2012

```Hi,

It is not vectrorizd per se. It is a C loop, which is faster than Python
ones.
The full matrix has a jump at half the size of the sliced matrix. My guess
is that it is a simple cache effect: with the full matrix, everything fits
in cache at 2000, but not at 4000, but for a "sliced" matrix (it is not a
real sliced matrix), 4000 means that the "slices" are the same size as the
full matrix at 2000.
What you see is just the blue line being twice as slow as the red one + the

Matthieu

2012/2/10 Alexander Kalinin <alec.kalinin@gmail.com>

> I found an interesting fact about element-wise matrix multiplication
> performance. I expected that full vectorized NumPy operations should always
> be faster then the loops. But it is not always true. Look at the code:
>
> import numpy as np
>
> import time
>
>
> def calc(P):
>
> for i in range(30):
>
> P2 = P * P
>
>  # full matrix
>
> N = 4000
>
> P = np.random.rand(N, N)
>
> t0 = time.time()
>
> calc(P)
>
> t1 = time.time()
>
> print " full matrix {:.5f} seconds".format(t1 - t0)
>
>
> # sliced matrix
>
> N = 2000
>
> P = np.random.rand(N, N)
>
> t0 = time.time()
>
> for i in range(4):
>
> calc(P)
>
> t1 = time.time()
>
> print " sliced matrix {:.5f} seconds".format(t1 - t0)
>
> The results are:
> full matrix 2.60245 seconds
> sliced matrix 1.49381 seconds
>
>
> I continue study of this case and found that the performance depends on
> matrix size. Look at the attached plot. The x-axis is the dimension of
> matrices, the y-axis is the execution time. Red line are the full matrix
> executions times, blue line are the sliced matrix execution times. The plot
> shows that the 2000 is the critical dimension that cause performance
>
> My configuration:
> OS: Ubuntu 11.10 (oneiric)
> CPU: Intel(R) Core(TM) i5 CPU M 480  @ 2.67GHz
> CPUs: 4
> Memory: 3746 MiB
> L2 cache: 3072 KB
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>

--
Information System Engineer, Ph.D.
Blog: http://matt.eifelle.com