Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Unsolved Weird execution times QImage by reference

    General and Desktop
    3
    6
    558
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • T
      Tony123 last edited by Tony123

      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 ??!!

      J.Hilk raven-worx 2 Replies Last reply Reply Quote 0
      • J.Hilk
        J.Hilk Moderators @Tony123 last edited by

        @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.

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

        Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        T 2 Replies Last reply Reply Quote 1
        • raven-worx
          raven-worx Moderators @Tony123 last edited by

          @Tony123
          see Implicit sharing

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply Reply Quote 1
          • T
            Tony123 @J.Hilk last edited by Tony123

            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 1 Reply Last reply Reply Quote 0
            • T
              Tony123 @J.Hilk last edited by

              @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.

              1 Reply Last reply Reply Quote 0
              • J.Hilk
                J.Hilk Moderators @Tony123 last edited by J.Hilk

                @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]

                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

                Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post