Skip to content

Instantly share code, notes, and snippets.

@michaeldorner
Last active February 22, 2023 13:47
Show Gist options
  • Save michaeldorner/c0fba1bc108fa230afd40d1d63d5df97 to your computer and use it in GitHub Desktop.
Save michaeldorner/c0fba1bc108fa230afd40d1d63d5df97 to your computer and use it in GitHub Desktop.
import timeit
import pandas as pd
import matplotlib.pyplot as plt
# measuring
numpy_results = {}
python_results = {}
config = {'number': 100, 'repeat': 100}
sizes = [2**exp for exp in range(12)] # or np.arange(12) ;-)
for size in sizes:
numpy_results[size] = min(timeit.repeat(f'np.arange({size})', setup='import numpy as np', **config))
python_results[size] = min(timeit.repeat(f'list(range({size}))', **config))
df = pd.concat((pd.Series(numpy_results, name='np.arange'), pd.Series(python_results, name='range')), axis=1)
# plotting
fig, ax = plt.subplots()
df.plot(ax=ax)
ax.set_xscale('log', base=2)
ax.set_xticks(sizes)
ax.set_xticklabels(sizes)
ax.set_xlabel('Size')
ax.set_ylabel('Best runtime for 10 runs out of 10 repetition')
@michaeldorner
Copy link
Author

michaeldorner commented Feb 22, 2023

output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment