pyqtgraph/examples/PlotSpeedTest.py

53 lines
1.2 KiB
Python
Raw Normal View History

#!/usr/bin/python
# -*- coding: utf-8 -*-
2013-02-25 04:09:03 +00:00
"""
Update a simple plot as rapidly as possible to measure speed.
"""
2021-02-11 22:14:02 +00:00
## Add path to library (just for examples; you do not need this)
import initExample
2021-02-11 22:14:02 +00:00
from collections import deque
2012-03-02 03:53:52 +00:00
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
from time import perf_counter
2021-02-11 22:14:02 +00:00
app = pg.mkQApp("Plot Speed Test")
p = pg.plot()
2013-02-25 04:09:03 +00:00
p.setWindowTitle('pyqtgraph example: PlotSpeedTest')
2021-02-11 22:14:02 +00:00
p.setRange(QtCore.QRectF(0, -10, 5000, 20))
p.setLabel('bottom', 'Index', units='B')
curve = p.plot()
2021-02-11 22:14:02 +00:00
data = np.random.normal(size=(50, 5000))
ptr = 0
rollingAverageSize = 1000
2021-02-11 22:14:02 +00:00
elapsed = deque(maxlen=rollingAverageSize)
def update():
2021-02-11 22:14:02 +00:00
global curve, data, ptr, elapsed, ptr
ptr += 1
2021-02-11 22:14:02 +00:00
# Measure
t_start = perf_counter()
curve.setData(data[ptr % 10])
app.processEvents(QtCore.QEventLoop.ProcessEventsFlag.AllEvents)
elapsed.append(perf_counter() - t_start)
# update display every 50-updates
if ptr % 50 == 0:
average = np.mean(elapsed)
fps = 1 / average
p.setTitle('%0.2f fps - %0.1f ms avg' % (fps, average * 1_000))
timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(0)
if __name__ == '__main__':
2021-05-13 21:28:22 +00:00
pg.exec()