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

Finding by QStringLiteral much faster than QLatin1String

    XMLWordPrintable

Details

    • Bug
    • Resolution: Out of scope
    • Not Evaluated
    • None
    • 5.5.0
    • None

    Description

      From

      https://bugs.kde.org/show_bug.cgi?id=361691

      #include <QString>
      #include <QDebug>
      
      #define N 100000000
      
      bool testQStringLiteral()
      {
          qDebug() << "QStringLiteral";
          bool result = false;
          QString s;
          for (int i = 0; i < N; ++i)
          {
              result |= s.contains(QStringLiteral("foo"));
          }
          return result;
      }
      
      bool testQLatin1String()
      {
          qDebug() << "QLatin1String";
          bool result = false;
          QString s;
          for (int i = 0; i < N; ++i)
          {
              result |= s.contains(QLatin1String("foo"));
          }
          return result;
      }
      
      int main(int argv, char **)
      {
          if (argv == 1) {
              return testQLatin1String();
          } else {
              return testQStringLiteral();
          }
      }
      
       
      $ time ./qttester 
      QLatin1String
      
      real    0m1.450s
      user    0m1.448s
      sys     0m0.004s
      $ time ./qttester 1
      QStringLiteral
      
      real    0m0.655s
      user    0m0.652s
      sys     0m0.000s
      
      

      Are the current guidelines about when to use QLatin1String and QStringLiteral in line with this? I thought QLatin1String should be used when an overload for it exists.

      Or is this a QString bug?

      Attachments

        For Gerrit Dashboard: QTBUG-52617
        # Subject Branch Project Status CR V

        Activity

          People

            thiago Thiago Macieira
            steveire Stephen Kelly (Personal)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes