Skip to content

Instantly share code, notes, and snippets.

@j9ac9k
Last active June 9, 2022 04:29
Show Gist options
  • Save j9ac9k/59834d527d145b6f4b11fa930eac71d8 to your computer and use it in GitHub Desktop.
Save j9ac9k/59834d527d145b6f4b11fa930eac71d8 to your computer and use it in GitHub Desktop.
benchmark drawPoints vs. drawPointsNp
from PySide6 import QtCore, QtGui
import shiboken6
import numpy as np
from time import perf_counter
class Points:
def __init__(self, use_drawNp):
self.use_drawNp = use_drawNp
self.alloc(0)
def alloc(self, size):
self.arr = np.empty((size, 2), dtype=np.float64)
if self.use_drawNp:
self.objs = self.arr
else:
self.objs = shiboken6.wrapInstance(self.arr.ctypes.data, QtCore.QPointF)
def get(self, size):
if size != self.arr.shape[0]:
self.alloc(size)
return self.objs, self.arr
def generate_pattern(npoints, size):
rng = np.random.default_rng()
x = rng.random(npoints) * size
y = rng.random(npoints) * size
arr = np.empty((npoints, 2), dtype=np.float64)
arr[:, 0] = x
arr[:, 1] = y
return arr
npoints = 10_000
size = 500
nframes = 100
pattern = generate_pattern(npoints, size)
def run(use_drawNp):
# generate lines once
segments = Points(use_drawNp)
points, memory = segments.get(npoints)
memory[:] = pattern
# draw multiple frames using the same lines array
t0 = perf_counter()
for _ in range(nframes):
qimg = QtGui.QImage(size, size, QtGui.QImage.Format.Format_RGB32)
qimg.fill(QtCore.Qt.GlobalColor.transparent)
painter = QtGui.QPainter(qimg)
painter.setPen(QtCore.Qt.GlobalColor.cyan)
if use_drawNp:
painter.drawPointsNp(memory[:,0], memory[:,1])
else:
painter.drawPoints(points, npoints)
painter.end()
t1 = perf_counter()
return t1 - t0
for use_drawNp in [False, True]:
duration = run(use_drawNp)
fps = int(nframes / duration)
print(f'{use_drawNp=} {duration=:.3f} {fps=}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment