Skip to content

Commit

Permalink
Added performances tips
Browse files Browse the repository at this point in the history
  • Loading branch information
rougier committed Jun 25, 2020
1 parent 33e1ee4 commit 1d04c1b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ fonts*
*.log
*.out
*.aux
figures/*.pdf
17 changes: 17 additions & 0 deletions cheatsheets.tex
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,23 @@
\hspace*{2.5mm}~$\rightarrow$ plt.close("all")
\end{myboxed}

\begin{myboxed}{Performance tips}
{\ttfamily \fontsize{6pt}{7pt}\selectfont
\textcolor{red}{ax.scatter(X, Y) \hfill slow}\\
ax.plot(X, Y, marker="o", ls="") \hfill fast\\
\hrule \smallskip
\textcolor{red}{for i in range(0,n,2): \hfill (very) slow}\\
\hspace*{2.5mm}~\textcolor{red}{plt.plot(X[i:i+2], Y[i:i+2])}\\

X0, Y0 = X[0::2], Y[0::2] \hfill fast\\
X1, Y1 = X[1::2], Y[1::2]\\
S = [None]*len(X)\\
X = [v for t in zip(X0,X1,S) for v in t]\\
Y = [v for t in zip(Y0,Y1,S) for v in t]\\
plt.plot(X,Y)
}
\end{myboxed}

\end{multicols*}
\end{document}

52 changes: 52 additions & 0 deletions scripts/performance-tips.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -----------------------------------------------------------------------------
# Matplotlib cheat sheet
# Released under the BSD License
# -----------------------------------------------------------------------------
import time
import numpy as np
import matplotlib.pyplot as plt


fig, ax = plt.subplots()

n = 10_000_000
np.random.seed(1)
X = np.random.uniform(0,1,n)
Y = np.random.uniform(0,1,n)

start = time.perf_counter()
ax.plot(X, Y, marker="o", ls="")
end = time.perf_counter()
print(f"Time: {end-start}s")

ax.clear()

start = time.perf_counter()
ax.scatter(X, Y)
end = time.perf_counter()
print(f"Time: {end-start}s")

ax.clear()

n = 10_000
np.random.seed(1)
X = np.random.uniform(0,1,n)
Y = np.random.uniform(0,1,n)

start = time.perf_counter()
for i in range(0,n,2): plt.plot(X[i:i+2], Y[i:i+2])
end = time.perf_counter()
print(f"Time: {end-start}s")

ax.clear()

start = time.perf_counter()
X0,Y0 = X[0::2], Y[0::2]
X1,Y1 = X[1::2], Y[1::2]
S = [None]*len(X)
X = [v for t in zip(X0,X1,S) for v in t]
Y = [v for t in zip(Y0,Y1,S) for v in t]
plt.plot(X,Y)
end = time.perf_counter()
print(f"Time: {end-start}s")

0 comments on commit 1d04c1b

Please sign in to comment.