Skip to content

Instantly share code, notes, and snippets.

@jakelevi1996
Last active December 31, 2019 01:02
Show Gist options
  • Save jakelevi1996/aac4a919dcd22f6f6a71cdfc9ac698dc to your computer and use it in GitHub Desktop.
Save jakelevi1996/aac4a919dcd22f6f6a71cdfc9ac698dc to your computer and use it in GitHub Desktop.
Speed tests

Speed tests

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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment