PySide6 Timing differences
13:16 01 Jan 2026

I'm building a grid of labels that I can then continually change the background colors as values of the grid location changes. I have two different ways of getting the label in the specific grid location and then change its color (ChangeBox1 and ChangeBox2).

When I call these routines while in the class init function, they run substantially faster than when I call them after displaying the window.

Why?

I'm using python 3.10.15

Average execution time of ChangeBox1 in init over 500 runs: 9.419159032404423e-07 seconds
Average execution time of ChangeBox2 in init over 500 runs: 4.6449992805719374e-07 seconds
Average execution time of ChangeBox1 after window.show over 500 runs: 1.5272999880835415e-05 seconds
Average execution time of ChangeBox2 after window.show over 500 runs: 1.4401915948837996e-05 seconds

Here's my MWE:

import timeit

from PySide6.QtCore import QSize, Qt
from PySide6.QtGui import QColor
from PySide6.QtWidgets import (
    QApplication,
    QGridLayout,
    QLabel,
    QMainWindow,
    QWidget,
)


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("My App")
        self.setMinimumSize(QSize(400, 300))

        self.layout = QGridLayout()
        self.layout.setSpacing(3)

        xMax = 4
        yMax = 5
        steps = xMax * yMax
        midStep = steps // 2 + 1
        cStep = 255 // midStep
        cRed = 255
        cGreen = 0
        cBlue = 0
        self.labels = {}
        for b in range(steps):
            label = f"T{b}"
            (x, y) = divmod(b, yMax)
            color = f"{cRed:02x}{cGreen:02x}{cBlue:02x}"
            self.labels[(x, y)] = self.AddBox(color, label, x, y)

            if b < midStep:
                cGreen += cStep
            else:
                cRed -= cStep

        widget = QWidget()
        widget.setLayout(self.layout)
        self.setCentralWidget(widget)

        runs = 500
        time1 = timeit.timeit(lambda: self.ChangeBox1(2, 2), number=runs)
        time2 = timeit.timeit(lambda: self.ChangeBox2(1, 1), number=runs)

        print(
            f"Average execution time of ChangeBox1 in init over {runs} runs: {time1 / runs} seconds"
        )
        print(
            f"Average execution time of ChangeBox2 in init over {runs} runs: {time2 / runs} seconds"
        )

        # Find the fastest way to change the color of a specific widget

    def ChangeBoxColor(self):
        self.ChangeBox1(2, 2)
        self.ChangeBox2(1, 1)

        runs = 500
        time1 = timeit.timeit(lambda: self.ChangeBox1(2, 2), number=runs)
        time2 = timeit.timeit(lambda: self.ChangeBox2(1, 1), number=runs)

        print(
            f"Average execution time of ChangeBox1 after window.show over {runs} runs: {time1 / runs} seconds"
        )
        print(
            f"Average execution time of ChangeBox2 after window.show over {runs} runs: {time2 / runs} seconds"
        )

    def MyLabel(self, color, text):
        tBox = QLabel(text)
        tBox.setAlignment(Qt.AlignCenter)
        tBox.setStyleSheet(f"background: #{color}; font-style: bold; font-size: 24pt")

        return tBox

    def AddBox(self, color, text, x, y):
        box = self.MyLabel(color, text)
        self.layout.addWidget(box, x, y)

        return box

    def ChangeBox1(self, x, y):
        cell = self.layout.itemAtPosition(x, y).widget()
        cell.setStyleSheet(
            f"background: #{'0000ff'}; font-style: bold; font-size: 24pt"
        )

    def ChangeBox2(self, x, y):
        self.labels[(x, y)].setStyleSheet(
            f"background: #{'00f0ff'}; font-style: bold; font-size: 24pt"
        )


app = QApplication(sys.argv)
window = MainWindow()
window.show()
window.ChangeBoxColor()
app.exec()
python pyside pyside6-gui