Skip to content

Instantly share code, notes, and snippets.

@ayorgo
Created June 7, 2020 20:05
Show Gist options
  • Save ayorgo/c6adafd96f939292c55c3eb4affd8dc6 to your computer and use it in GitHub Desktop.
Save ayorgo/c6adafd96f939292c55c3eb4affd8dc6 to your computer and use it in GitHub Desktop.
Computational performance measurement tool
from typing import List
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from timeit import repeat
from multiprocessing import Pool, cpu_count
def time_inline(function: str, *arg) -> float:
stmt = f'{function}(*{arg})'
return min(repeat(stmt, repeat=5, number=1, globals=globals()))
def time_it(functions: List[str], args):
with Pool(cpu_count()) as pool:
iterator = ((function, arg) for function in functions for arg in args)
results = pool.starmap(time_inline, iterator)
data = np.reshape(results, (len(functions), len(args)))
result = pd.DataFrame(index=functions, columns=args, data=data)
ax = result.T.plot()
ax.set_xlabel('input size')
ax.set_ylabel('time (seconds)');
ax = result.div(result.min()).T.plot(loglog=True)
ax.set_xlabel('input size')
ax.set_ylabel('time (relative)');
functions = ['function1',
'function2',
'function3']
args = [10, 50, 100, 500, 1_000, 5_000, 10_000]
time_it(functions, args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment