Results 1 to 5 of 5

Thread: What is faster, QRegExp, or QByteArray::indexOf

  1. #1
    Join Date
    Mar 2010
    Location
    Capelle aan den IJssel, Netherlands
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default What is faster, QRegExp, or QByteArray::indexOf

    Hello all,

    I am trying to do some stream parsing, in where I would like to be able to parse HTTP headers, but this question i guess is more in general.

    The parsing at least is time critical, so I want the fastest way to do this. String comparison is always a nasty thing. What would be i.e. the fastest way, to find the end of the HTTP header section (the empty CRLF line).

    I guess, I could do it, by searching for the CRLF in the data stream part I just read. Or I could use a regular expression. QRegExp probably uses string comparison, as QByteArray may compare by byte values, which would be faster??

    Or any other ideas? Has someone encountered this problem before and can give me some heads up on what would be the best solution here?

    Thanks

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

    Default Re: What is faster, QRegExp, or QByteArray::indexOf

    I would expect them to be more or less equally fast in this particular situation.
    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. #3
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: What is faster, QRegExp, or QByteArray::indexOf

    Impossible to say, without knowing the implementation details of the regexp package you're using. The only way to be sure would be testing in a profiler.

    Note that there are a number of string-searching algorithms that are faster than brute-force linear search (look up the Boyer-Moore algorithm, for example). Good regexp packages tend to use such algorithms, but the regexp object itself introduces some overhead.

    Honestly, I would code up the simplest thing I could think of, then run the whole program through a profiler ONLY IF performance was a demonstrable issue. Only at that point would I consider retooling the string search, and then only if it proved to be a significant bottleneck. Premature optimization is the root of all evil.

  4. The following user says thank you to SixDegrees for this useful post:

    _Stefan (15th September 2010)

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

    Default Re: What is faster, QRegExp, or QByteArray::indexOf

    If he's searching for a two character long string then sophisticated algorithms are useless. Also the regexp won't have any benefits here, compilation of the state machine will add some overhead which will negate potential benefits of using the regular expression. I'd go for the simplest available solution which is using QByteArray::indexOf().
    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
    Mar 2010
    Location
    Capelle aan den IJssel, Netherlands
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: What is faster, QRegExp, or QByteArray::indexOf

    I created a little test application, to see the difference between searching methods.

    I am catching the stream data from a QIODevice, so that gives me a QByteArray.
    I tried fourways of searching in the QByteArray::indexOf, passing a QRegExp, QString, QByteArray and a const char*.

    Here are the results, if I do 100.000 simple searches for CRLFCRLF in a QByteArray (times in msec)
    testRegExp: 812
    testString: 266
    testByte: 204
    testChar: 140

    So I guess the most simple and fast way, is to just use the const char* overload of indexOf.
    I'm guessing that is going to be fast enough.

    Thanks for thinking with me
    Maybe this test result can help someone else someday!

Similar Threads

  1. QList<A*> indexOf
    By Moppel in forum General Programming
    Replies: 6
    Last Post: 8th August 2017, 14:17
  2. Replies: 9
    Last Post: 25th July 2009, 13:27
  3. QString::indexOf Qt::CaseInsensitive
    By Carlsberg in forum Qt Programming
    Replies: 3
    Last Post: 19th May 2009, 18:11
  4. Faster paintEvent
    By SailinShoes in forum Qt Programming
    Replies: 1
    Last Post: 1st October 2008, 15:25
  5. Faster code possible?
    By ShaChris23 in forum General Programming
    Replies: 1
    Last Post: 28th May 2008, 07:35

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.