Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-9043

Make a flavor of QString::split() which splits by a collection of chars

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Unresolved
    • P4: Low
    • None
    • 4.6.2
    • None
    • Visual Studio 2008 SP1

    Description

      Currently the only way to split a string by any white space is to use a regular expression. This is a constly operation and can be replaced by a much simpler flavor of split which works on a collection of chars of just white spaces.

      Here's a short example of the performance difference between split with a QRegExp and just a single char:

      #include <QTime>
      int main(int argc, char *argv[])
      {
      	QString s1 = "This is a long sentence\twith several\t spaces and tabs and some new\nlines\r\nas separators which needs to be split\n";
      	QString s2 = "This is a long sentence with several spaces and tabs and some new lines as separators which needs to be split";
      
      	QTime timer;
      	timer.start();
      	QRegExp reg("\\s+");
      	for(int i = 0; i < 10000; ++i)
      	{
      		QStringList sl = s1.split(reg, QString::SkipEmptyParts);
      	}
      	printf("QRegExp took: %d sec\n", timer.elapsed());
      
      	timer.start();
      	for(int i = 0; i < 10000; ++i)
      	{
      		QStringList sl = s2.split(' ', QString::SkipEmptyParts);
      	}
      	printf("space took: %d sec\n", timer.elapsed());
      	return 0;
      }
      

      In my computer this produces:

      QRegExp took: 548 sec
      space took: 156 sec

      QRegExp is more than 3 times slower.
      Splitting by a collection of chars doesn't have the complexity of using a regular expression and should be closer to the split by space

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            Unassigned Unassigned
            shoosh Shy Shalom
            Votes:
            4 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes