Skip to content

Instantly share code, notes, and snippets.

@anntzer
Created July 30, 2017 00:16
Show Gist options
  • Save anntzer/9192485c025933cc233f3a5dc74de936 to your computer and use it in GitHub Desktop.
Save anntzer/9192485c025933cc233f3a5dc74de936 to your computer and use it in GitHub Desktop.
Time per edge in plotting lines with Matplotlib.
import time
from matplotlib import pyplot as plt
import numpy as np
ns = [10, 30, 100, 300, 1000, 3000, 10000]
all_times = []
for n in ns:
print(f"{n} edges")
fig, ax = plt.subplots()
ax.set(xticks=[], yticks=[])
for spine in ax.spines.values():
plt.setp(spine, visible=False)
data = np.random.RandomState(0).random_sample((2, n + 1))
ax.plot(*data, antialiased=True, solid_joinstyle="miter")
def profile(max_time=1, timer=time.perf_counter):
times = []
base_start = timer()
while timer() - base_start < max_time:
start = timer()
fig.canvas.draw()
elapsed = timer() - start
times.append(elapsed)
return times
times = profile()
all_times.append(times)
plt.close("all")
fig, axs = plt.subplots(len(ns), sharex=True)
min_time = np.min(np.concatenate(all_times))
max_time = np.max(np.concatenate(all_times))
bins = np.geomspace(.99 * min_time, 1.01 * max_time)
for ax, n, times in zip(axs, ns, all_times):
ax.hist(times, bins, normed=True)
ax.set(xlabel="$t$ (s)", xscale="log")
ax.text(.95, .95, f"{n} edges (N={len(times)})",
ha="right", va="top", transform=ax.transAxes)
ax.label_outer()
fig, ax = plt.subplots()
# Use the minimum time as aggregate.
ax.plot(ns, [np.min(times) / n for n, times in zip(ns, all_times)], "o")
ax.set(xlabel="number of edges", ylabel="time per edge (s)",
xscale="log", yscale="log")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment