Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Weird execution times QImage by reference
Qt 6.11 is out! See what's new in the release blog

Weird execution times QImage by reference

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.3k Views 1 Watching
  • 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 Offline
    T Offline
    Tony123
    wrote on last edited by Tony123
    #1

    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.HilkJ raven-worxR 2 Replies Last reply
    0
    • T 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.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @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


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

      T 2 Replies Last reply
      1
      • T 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 ??!!

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        @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
        1
        • J.HilkJ J.Hilk

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

          T Offline
          T Offline
          Tony123
          wrote on last edited by Tony123
          #4

          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.HilkJ 1 Reply Last reply
          0
          • J.HilkJ J.Hilk

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

            T Offline
            T Offline
            Tony123
            wrote on last edited by
            #5

            @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
            0
            • T 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.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by J.Hilk
              #6

              @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


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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved