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. Fastest way to compare QVariant with UserType

Fastest way to compare QVariant with UserType

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.1k 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.
  • H Offline
    H Offline
    H.Klingel
    wrote on last edited by
    #1

    I'm looking for a very fast way to compare two QVariants that wrap the same user type.

    What do you think about the following approaches:
    @bool operator==() const(const QVariant &rhs)
    {
    if(myData.userType() == qMetaTypeId<MyUserType>())
    {
    bool method1 = static_cast<MyUserType>(myData.constData()) == static_cast<const MyUserType>(rhs.myData.constData());

    bool method2 = myData.value<MyUserType>() == rhs.myData.value<MyUserType>();
    }
    return something
    }@

    I would think that the first approach is faster, because no deep copy or ctor call is necessary. It is more risky because of the static_cast and kind of a hack though.

    I took a look into the Qt implementation of the second method, but as far as I could see, at least once the copy ctor of MyUserType is called.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Well... why don't you try and see? Seems like something to just try out and doing some measurements on.

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        i would also tend to the second comparison.
        But you should add "QVariant::canConvert()":http://qt-project.org/doc/qt-4.8/qvariant.html#canConvert checks before comparing the values.

        --- 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
        0
        • H Offline
          H Offline
          H.Klingel
          wrote on last edited by
          #4

          Thanks for the answers guys.

          I took the advice to compare the two approaches by stepping through them. The result is pretty clear.
          The first approach first calls QVariant::constData() twice (for each side) and then MyClass::operator==() is called.

          The second approach takes several steps, including multiple checks and a ctor call for each side before !!! method 1 is used (with a reinterpret_cast instead of a static_cast though).

          I think I'll stick to the first method, because it is faster and the comparing of the MetaTypeIds beforehand should in my case be enough to avoid illegal static_casts.

          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