Shown below is some code to perform generic speed tests and save a log-log graph of the results (in this example, eigenvalue decomposition for SPD matrices is being tested). The resulting graph is shown below the code:
import numpy as np
import matplotlib.pyplot as plt
import scipy as sp
from time import perf_counter
# List of input sizes to try
n_list = np.logspace(1, 3, 20, dtype=np.int)
# Number of times to repeat the experiment
n_repeats = 5
results = np.zeros([n_list.size, n_repeats])
print("Number of sizes to try:\n", n_list, "\n\nExperiment progress:")
# Loop through input sizes
for i_n, n in enumerate(n_list):
print("n =", n, end="")
# Loop through repeats
for repeat in range(n_repeats):
print(", r = ", repeat, end="", flush=True)
# Generate random SPD matrix
A = np.random.normal(size=[n, n])
A = A.T.dot(A)
# Perform eigenvalue decomposition
t_start = perf_counter()
np.linalg.eigh(A)
t = perf_counter() - t_start
results[i_n, repeat] = t
print()
print("\nResults:\n", results)
plt.figure(figsize=[8, 6])
plt.loglog(n_list, results, "bo", alpha=0.5)
plt.grid(True)
plt.xlabel("Matrix rank")
plt.ylabel("Time (s)")
plt.title("Time taken to perform eigenvalue decomposition")
plt.savefig("Eigenvalue Results")
plt.close()