Qt World Summit: Register Today!

Measuring frame rendering time

  • Hello.

    I have an application using QOpenGLWidget, in its paintGL() method I measure the frame rendering time:

    void MainOpenGLWidget::paintGL()
        sceneManager->callInitSceneAndOptionsWidget(this->width(), this->height());
        QElapsedTimer framerateTime;
        lastFrameRenderTimeInNanoseconds = framerateTime.nsecsElapsed();

    In this case, I get a strangely different frame rendering time, from 0.1 to 0.3 milliseconds.
    The description of the update() method says:
    "This function does not cause an immediate repaint; instead it schedules a paint event for processing when Qt returns to the main event loop."

    How do I measure the frame rendering time?

  • two things:

    • clock resolution is platform dependent so don't expect microsecond or nanosecond precision unless you know for a fact that your clock supports it

    • you are using a mult-tasking system that may preempt your code, so unless you disable interrupts while generating your frame, the timing of that frame generation shouldn't be considered highly accurate.

    The long and short of it is that you cannot measure frame render efficiency the way you are trying to. About as close as you can get would be to iteratively accumulate thousands of render times, then subtract the percentage of CPU time that is not spent on the actual render, and then divide it out to get an "average" render time.

  • This post is deleted!

  • @Kent-Dorfman Describe in more detail how you suggest I measure the frame rendering time?

  • @mrjbom

    QTimer timer;
    //do your rendering here
    qDebug() << timer.elapsed();

    Accuracy of this, however, like @Kent-Dorfman mentioned, is platform-dependent.

  • This post is deleted!

  • @artwaw, How does your timer differ from the one I use?
    I believe it will also give values with a large difference.
    In addition, I am interested in exactly in microseconds.

  • @mrjbom said in Measuring frame rendering time:

    As per QT Doc

    If you need to trigger a repaint from places other than paintGL() 
    (a typical example is when using timers to animate scenes),
     you should call the widget's update() function to schedule an update.

    I feel if you want to measure the frame rendering time

    void MainOpenGLWidget::paintGL()
    QElapsedTimer framerateTime;

    sceneManager->callInitSceneAndOptionsWidget(this->width(), this->height());
    lastFrameRenderTimeInNanoseconds = framerateTime.nsecsElapsed();


  • I just noticed something. You should not call update() from within paintGL(). It works the other way around: update(), whether called implicitly or explicitly, calls paintGl() to render the scene...paintGL() is a virtual callback, not a slot.

    void paintGl() {
        // do stuff
        render_time = now() - begin_time

  • @Kent-Dorfman, do you suggest not calling update() at all?
    If I don't call it from paintGL() or other places, then my frames don't update(sometimes abruptly update themselves).

  • @Kent-Dorfman, @nagesh, The documentation says: The signals aboutToCompose() and frameSwapped() will be emitted when the composition is starting and ending.

    What happens if I start measuring the time when aboutToCompose() happens and end the change when frameSwapped() happens, so I get the frame rendering time?

  • @mrjbom calling update will schedule the paint event for the processing which means internally it triggers paintGL().
    For constant update of frames you can do it with the QTimer slot call update()
    or as soon as new frame is available call update()

    As per QT doc
    in frameSwapped()

    This signal is emitted after the potentially blocking buffer swap has been done. 
    Applications that wish to continuously repaint synchronized to the vertical refresh,
     should issue an update() upon this signal. This allows for a much smoother experience 
    compared to the traditional usage of timers.

    I feel frame rendering is happening in paintGL()

  • @nagesh said in Measuring frame rendering time:

    I feel frame rendering is happening in paintGL()

    In paintGL, the frame is being prepared. So it is useless to measure time there.

  • @mrjbom read the description for paintGL()
    it says.. paintGL() - Renders the OpenGL scene. Gets called whenever the widget needs to be updated.

    What does your function
    sceneManager->callDrawScene() doing?

  • @nagesh It draws the current scene. The usual functions that are usually found in paintGL().

    In any case, if I measure the time only within paintGL (), then the time is constantly different from the previous one.
    Probably because QElapsedTimer reads the time within the operating system, not my application, so due to the task switching by the operating system, it is different.
    How can I measure the time regardless of the task switching by the operating system?

  • @mrjbom There is class in qt QOpenGLTimerQuery

    Whether it's applicable for your requirement not sure.

  • @mrjbom Dude! We've been over this. You cannot accurately measure the time spent in a call that takes nanoseconds or microseconds to complete. you are not using a realtime OS. Either research the science of RTOS or give up on getting consistent numbers.

  • @nagesh Got it! This is what I need.
    To be more precise, QOpenGLTimeMonitor () helped me.
    Thank you for your help.

Log in to reply