Compares matrix multiplication implementations with timeit

numpy has a very fast implementation of matrix multiplication. There are many ways to be slower. The following uses timeit to compare implementations.

Compared implementations:

Preparation

import timeit
import numpy

from teachcompute.validation.cython.td_mul_cython import (
    multiply_matrix,
    c_multiply_matrix,
    c_multiply_matrix_parallel,
    c_multiply_matrix_parallel_transposed as cmulparamtr,
)


va = numpy.random.randn(150, 100).astype(numpy.float64)
vb = numpy.random.randn(100, 100).astype(numpy.float64)
ctx = {
    "va": va,
    "vb": vb,
    "c_multiply_matrix": c_multiply_matrix,
    "multiply_matrix": multiply_matrix,
    "c_multiply_matrix_parallel": c_multiply_matrix_parallel,
    "c_multiply_matrix_parallel_transposed": cmulparamtr,
}

Measures

numpy

res0 = timeit.timeit("va @ vb", number=100, globals=ctx)
print("numpy time", res0)
numpy time 0.067679797000892

python implementation

res1 = timeit.timeit("multiply_matrix(va, vb)", number=10, globals=ctx)
print("python implementation", res1)
python implementation 7.336435834993608

cython implementation

res2 = timeit.timeit("c_multiply_matrix(va, vb)", number=100, globals=ctx)
print("cython implementation", res2)
cython implementation 0.12964998700044816

cython implementation parallelized

res3 = timeit.timeit("c_multiply_matrix_parallel(va, vb)", number=100, globals=ctx)
print("cython implementation parallelized", res3)
cython implementation parallelized 0.5565220380012761

cython implementation parallelized, AVX + transposed

res4 = timeit.timeit(
    "c_multiply_matrix_parallel_transposed(va, vb)", number=100, globals=ctx
)
print("cython implementation parallelized avx", res4)
cython implementation parallelized avx 0.014571653002349194

Speed up…

print(f"numpy is {res1 / res0:f} faster than pure python.")
print(f"numpy is {res2 / res0:f} faster than cython.")
print(f"numpy is {res3 / res0:f} faster than parallelized cython.")
print(f"numpy is {res4 / res0:f} faster than avx parallelized cython.")
numpy is 108.399200 faster than pure python.
numpy is 1.915638 faster than cython.
numpy is 8.222868 faster than parallelized cython.
numpy is 0.215303 faster than avx parallelized cython.

Total running time of the script: (0 minutes 8.139 seconds)

Gallery generated by Sphinx-Gallery