[PySide] Very slow items removing from QGraphicsScene

Evgeny esp.home at gmail.com
Sat Mar 8 16:35:10 CET 2014


Hi everyone,

My program should work with a great number of graphic items in the 
graphic scene QGraphicsScene. There are hundred thousand homogeneous 
items similar to QGraphicsRectItem. The items can be removed or added at 
the runtime. All the items sometimes are removed from the scene.

I have noticed that if I want to remove all the items from the scene 
using “clear” method or in the loop using “removeItem” method (“clear” 
works much longer), I have to wait for a long time the operation 
completing, if the cleaning is used not for the first time. In this 
case, the item removing, unlike the element adding that is always the 
same, is extremely slow. Such a situation occurs when there are more 
than several thousand items (more than 10 000). The time for the 
removing items increases exponentially (see the pictures with the 
attached plots). Also I have written a simple test example (please, find 
the attached example), demonstrating this problem.

At present I use PySide, version 1.2.1 (Qt 4.8.5) and Python 2.7.6 
x86/x64 on OS Windows 7 x64.

CPU: Intel Core 2 Quad 6600
RAM: 6 Gb

--
Best regards,
Evgeny
-------------- next part --------------
#coding=utf-8

"""
Simple benchmark items removing from QGraphicScene

"""

import sys
import time

from PySide.QtGui import QApplication
from PySide.QtGui import QGraphicsScene


def elapsed_time(func):
    def wrapper(*args):
        t = time.time()
        try:
            ret = func(*args)
        except:
            raise
        finally:
            print '({}) Elapsed time: {:.3f} sec.'.format(func.func_name,
                                                          time.time() - t)
        return ret
    return wrapper


@elapsed_time
def add_items(scene, items, n):
    del items[:]
    for i in xrange(n):
        item = scene.addRect(i, i, 1, 1)
        items.append(item)


@elapsed_time
def remove_items(scene, items):
    for item in items:
        scene.removeItem(item)
    del items[:]


#===============================================================================
if __name__ == '__main__':
    app = QApplication(sys.argv)

    scene = QGraphicsScene()
    items = []

    n = 50000

    # First run (fast)
    add_items(scene, items, n)
    remove_items(scene, items)

    # Second run (very long)
    add_items(scene, items, n)
    remove_items(scene, items)

    sys.exit()
-------------- next part --------------
A non-text attachment was scrubbed...
Name: bench_qgraphicsscene.png
Type: image/png
Size: 14530 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20140308/df82d6c6/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: bench_qgraphicsscene_semilogx.png
Type: image/png
Size: 12857 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20140308/df82d6c6/attachment-0001.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: bench_qgraphicsscene_semilogy.png
Type: image/png
Size: 16524 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20140308/df82d6c6/attachment-0002.png>


More information about the PySide mailing list