Sometimes yes, sometimes no. The two APIs QCanvas and Graphics View are similar enough in behavior and API to be compared like this, that's only reasonable. But the same code might not be as fast in one as the other, although often tweaking the slow one will bring them up to the same level. This is no exception.
In your example, you've got 10000 line items on top of each other. This is the absolute worst-case scenario for a BSP tree, which is what Graphics View uses for indexing. If you run a profiler, it'll show that most of the time is spent in the BSP tree.
First, the fastest the program can start, show itself, and then quit, is today 4.5 seconds on my machine using Qt 4.3. With Qt 3.3, this is 1.2 seconds. Now,
scene.setBspTreeDepth(6); // seems to be optimal for your app
scene.setBspTreeDepth(6); // seems to be optimal for your app
To copy to clipboard, switch view to plain text mode
Now it takes only 0.8 seconds with 4.3, a 5.6x speedup. That's 50% faster than Qt 3. You should notice right away that the Qt 4 app starts faster than the Qt 3 one.
Now, you're creating lines that have a width of 1. In Qt 3, line items were just drawn as cosmetic lines (startpoint, endpoint). In Qt 4, line items follow the same capping rules as QPainter lines, and they transform properly. Try zooming in in the Qt 3 example, you'll see the lines don't get any thicker, but in Qt 4 they do. Their shape gets slightly more complex with Qt 4 also, because the shape of a line in Qt 4 is 100% accurate, whereas in Qt 3 a QCanvasLine's area was only estimated roughly. Try setting a cosmetic pen instead of a normal pen in Qt 4:
i
->setPen
( QPen(QColor(qrand
()%32
*8,qrand
()%32
*8,qrand
()%32
*8),
0) );
// notice pen width == 0
i->setPen( QPen(QColor(qrand()%32*8,qrand()%32*8,qrand()%32*8), 0) );
// notice pen width == 0
To copy to clipboard, switch view to plain text mode
Because the cosmetic pen shape is so simple, and this is equivalent to QCanvas' behavior, you get close to a 10x speedup. Now the example runs at approximately the same speed in Qt 4 as in Qt 3.
There's still some things that can be optimized further in Qt 4, your example serves as a very nice benchmark. If you still find the example is slow, do like me and run a profiler on it (I recommend valgrind --tool=callgrind, and kcachegrind for visualizing the results).
Bookmarks