[Qt-jambi-interest] QPixmap erratic performance
Bart van Deenen
bvandeenen at infologic.nl
Mon Nov 17 11:55:14 CET 2008
Hi All, Gunnar
> There is also the fact that the precision of System.currentTimeMillis()
> is system dependent. On windows its precision is ~17ms on Linux I
> believe it is between 1 and 5ms, so you also have to factor that into
> benchmarks and run enough iterations for the precision to be of no
> consequence.
It's not the System.currentTimeMillis() that's causing me to look into this.
I'm having very unsmooth animation! I've managed to cause some erraticness in
my demo code by making many QPixmaps. It's still not as bad as my real code,
but that has a lot of recursive drawing into QPixmaps, i.e. there might be a
succession of QPixmap#5 drawing into QPixmap#4 which then draws into #3, 2 and
1, which finally gets put onto the screen. I wonder if that's the culprit?
Anyway here's some code with a lot of QPixmaps that's starting to show erratic
measurements (timer event intervals between 15 and 400 ms. I'll now go add
recursion into my demo, to see if it gets worse.
Thanks
Bart
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Random;
import com.trolltech.qt.core.QPoint;
import com.trolltech.qt.core.QTimer;
import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QColor;
import com.trolltech.qt.gui.QFont;
import com.trolltech.qt.gui.QPaintEvent;
import com.trolltech.qt.gui.QPainter;
import com.trolltech.qt.gui.QPixmap;
import com.trolltech.qt.gui.QWidget;
public class Test extends QWidget
{
private QTimer timer;
private int x, y;
private FunctionTimer paint_function_timer = new FunctionTimer();
private FunctionTimer event_function_timer = new FunctionTimer();
private QPixmap root_pixmap;
class Sprite
{
QPixmap pixmap;
QPoint pos;
}
final static Random randomgenerator = new Random();
private ArrayList<Sprite> sprites = new ArrayList<Sprite>();
public Test() throws Exception
{
super();
root_pixmap = new QPixmap(400, 600);
Sprite sprite;
for (int i=0;i<80;i++)
{
sprite = new Sprite();
sprite.pixmap = new QPixmap(300, 200);
sprite.pixmap.fill(QColor.fromHsv(randomgenerator.nextInt(360),
255, 255));
QPainter p = new QPainter(sprite.pixmap);
p.setBrush(QColor.black);
p.setFont(new QFont("arial", 0));
p.drawText(5, sprite.pixmap.height() - 5, "This is sprite " +
i++);
sprite.pos = new
QPoint(randomgenerator.nextInt(root_pixmap.width()),
randomgenerator.nextInt(root_pixmap
.height()));
sprites.add(sprite);
}
this.setGeometry(root_pixmap.rect());
timer = new QTimer(this);
timer.timeout.connect(this, "qtimer_event()");
timer.start(40);
}
public void paintEvent(QPaintEvent g)
{
paint_function_timer.end("calls between paintEvent");
FunctionTimer ft = new FunctionTimer();
QPainter painter = new QPainter();
painter.begin(this);
painter.drawPixmap(10, 30, root_pixmap);
painter.end();
ft.end("putting root.pixmap on screen");
}
protected void qtimer_event()
{
event_function_timer.end("timerEvent");
FunctionTimer ft = new FunctionTimer();
this.render();
ft.end("this.render(root) ");
update();
}
private void render()
{
root_pixmap.fill(QColor.transparent);
QPainter p = new QPainter(root_pixmap);
for (Sprite sprite : sprites)
{
sprite.pos.setX((sprite.pos.x() + 1) % (root_pixmap.width() -
sprite.pixmap.width()));
sprite.pos.setY((sprite.pos.y() + 1) % (root_pixmap.height() -
sprite.pixmap.height()));
p.drawPixmap(sprite.pos, sprite.pixmap);
}
p.end();
}
public static void main(String[] args) throws Exception
{
QApplication.initialize(args);
Test qtMain = new Test();
qtMain.show();
QApplication.exec();
}
public class FunctionTimer
{
long t0;
public FunctionTimer()
{
t0 = System.currentTimeMillis();
}
public void end(String f, Object... stringformat)
{
long t = System.currentTimeMillis();
long delta = t - t0;
t0 = t;
Class[] argTypes = { String.class, Object[].class };
try
{
Method a = String.class.getMethod("format", argTypes);
System.out.println(a.invoke(null, f, stringformat) + " took "
+ delta + "ms.");
}
catch (Exception e)
{
}
}
}
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt.nokia.com/pipermail/qt-jambi-interest/attachments/20081117/80531d5d/attachment.html
More information about the Qt-jambi-interest
mailing list