Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Weird execution times QImage by reference



  • Hi guys

    Take a look on following code the Image is a class member of type QImage.

    void ImageViewer::setImage(const QImage &newImage)
    {
    Image = newImage; // takes 108 milliseconds
    imageLabel->setPixmap(QPixmap::fromImage(Image)); / takes 58 milliseconds
    }

    because I don't need to set newImage to Image class member anymore ,I simply use reference of newImage and save time. BUT I WAS SUPRISED!!

    void ImageViewer::setImage(const QImage &newImage)
    {
    imageLabel->setPixmap(QPixmap::fromImage(newImage)); / takes 158 milliseconds
    }

    ITS TAKE SAME TIME ..WHAT I'M MISSING HERE ??!!


  • Moderators

    @Tony123

    the = operator does not do a deep copy of QImage, you have QImage image_copy = image.copy(); for that,

    Image = newImage; should take up next to no time at all as its only asome pointer shuffeling.


  • Moderators



  • Thank for response @J.Hilk . So your said if I make a Image = newImage; thats not a deep copy so when I creating pixmap from Image that's why it takes less time than creating a pixmap from newImage which is therefore more time consume? Am I right ?



  • @J.Hilk So I try what you said and it's doesn't matter if you are creating QImage by = or copy , the setPixmap is taking same time.


  • Moderators

    @Tony123 said in Weird execution times QImage by reference:

    Thank for response @J.Hilk . So your said if I make a Image = newImage; thats not a deep copy so when I creating pixmap from Image that's why it takes less time than creating a pixmap from newImage which is therefore more time consume? Am I right ?

    What I'm saying is that after the compiler is done, the difference between the 2 functions is only that, the refcount of the image-data is increased by 1 and the pointer of Image was changed.

    The time intensive part, as far as I know, is coming from the QPixmap::fromImage function

    That said ~100ms to create a Pixmap is a lot, is it a big image and do you do you tests in Debug or release mode, those make a huge difference. and how exactly did you meassure the time it takes ?

    I did some tests myself:

    void MainWindow::setImage(const QImage &img)
    {
        quint64 time1, time2, time3;
    
        elap.start();
        refImage = img;
        time1 = elap.nsecsElapsed();
    
        showImg->setPixmap(QPixmap::fromImage(refImage));
        time2 = elap.nsecsElapsed();
    
        QImage deepCopy = img.copy(0,0,img.width(),img.height());
        time3 = elap.nsecsElapsed();
    
        qDebug() << "total time:" << time3 << "[ns]" << endl
                 << "create RefImage" << time1 << "[ns]" << endl
                 << "Time to create Pixmap and set it" << time2-time1 << "[ns]" << endl
                 << "Time for deepcopy" << time3-time2<< "[ns]";
    }
    

    //Multiple runs, averaged out

    Result for release build
    total time: 111225 [ns]
    create RefImage 364 [ns]
    Time to create Pixmap and set it 62724 [ns]
    Time for deepcopy 48137 [ns]

    Result for debug build
    total time: 274963 [ns]
    create RefImage 364 [ns]
    Time to create Pixmap and set it 234849 [ns]
    Time for deepcopy 39750 [ns]


Log in to reply