Results 1 to 6 of 6

Thread: What's the fastest QVector operation

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default What's the fastest QVector operation

    Hello!

    In copying some large data (lets say from an array) to a QVector, what should be faster?

    Option 1:
    Qt Code:
    1. for (int aaa = 0; aaa < SIZE_TO_COPY; aaa++)
    2. {
    3. myVector.append(myArray[aaa]);
    4. }
    To copy to clipboard, switch view to plain text mode 

    Option 2:
    Qt Code:
    1. const int startPoint = myVector.size();
    2. const int lastPos = startPoint + SIZE_TO_COPY;
    3. myVector.resize(lastPos); //(myVector.size() + SIZE_TO_COPY)
    4.  
    5. for (int aaa = startPoint, bbb = 0; aaa < lastPos; aaa++, bbb++)
    6. {
    7. myVector.replace(aaa,myArray[bbb]);
    8. }
    To copy to clipboard, switch view to plain text mode 

    Modifier 1: Include myVector.reserve(myVector.size() + SIZE_TO_COPY); before the loop in the first option or before the resize() in the second option.
    Modifier 2: Use foreach(...) instead of for(...).

    Thanks,

    Momergil
    May the Lord be with you. Always.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: What's the fastest QVector operation

    Option 2 is faster than option 1.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    Momergil (29th May 2014)

  4. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: What's the fastest QVector operation

    You should probably also benchmark using an explicit memcpy from the source array to the target array.

    Cheers,
    _

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: What's the fastest QVector operation

    Provided that the data type stored can be copied using memcpy.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: What's the fastest QVector operation

    Quote Originally Posted by wysota View Post
    Provided that the data type stored can be copied using memcpy.
    Yeah, for my particular situation that inspired me to do this question, I'ld say memcpy is not allowed (the vector is a vector of double, and the array is an array of short). But in any case, how could I do a memcpy from an array to a QVector if their types were compatible? memcpy(&myVector,myArray,sizeof(myArray)) doesn't seem that would work.
    May the Lord be with you. Always.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: What's the fastest QVector operation

    You can safely copy doubles from one vector to another using memcpy. You cannot copy classes that have non-trivial constructors. memcpy() accepts addresses where it should copy to/from.

    Qt Code:
    1. memcpy(myVector.data(), myArray, length_of_array);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following user says thank you to wysota for this useful post:

    Momergil (30th May 2014)

Similar Threads

  1. sort a Qvector based on another Qvector
    By OzQTNoob in forum Qt Programming
    Replies: 2
    Last Post: 16th February 2012, 07:39
  2. Replies: 5
    Last Post: 3rd September 2011, 00:11
  3. Fastest PaintDevice?
    By rage in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 8th November 2007, 16:46
  4. What is the fastest way to draw a circle ?
    By Vladimir in forum Qt Programming
    Replies: 18
    Last Post: 6th September 2007, 18:26
  5. Fastest way to clear a QList
    By steg90 in forum Qt Programming
    Replies: 2
    Last Post: 8th June 2007, 10:26

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.