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

std::ranges::search is faster than QByteArrayView::contains

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • Not Evaluated
    • None
    • 6.7.0
    • None
    • Linux
    • Linux/X11
    • eeb6f0337 (dev), 7200f8638 (dev), 726379c58 (dev), 7886d0adb (dev)

    Description

      In the benchmark, std::ranges::search is faster than QByteArrayView::contains when comparing the same source and the same target.

      ********* Start testing of SpeedTest *********
      Config: Using QtTest library 6.7.0, Qt 6.7.0 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 13.2.1 20240206 [revision 67ac78caf31f7cb3202177e6428a46d829b70f23]), opensuse-tumbleweed 20240428
      QCRITICAL: SpeedTest::initTestCase() "%1"
      PASS   : SpeedTest::initTestCase()
      PASS   : SpeedTest::test_bytearray()
      RESULT : SpeedTest::test_bytearray():
           8,609 nsecs per iteration (total: 8,609, iterations: 1)
           1,307 CPU cycles per iteration, 152 MHz (total: 1,307, iterations: 1)
           1,171 instructions per iteration, 0.896 instr/cycle (total: 1,171, iterations: 1)
           255 branch instructions per iteration, 29.6 M/sec (total: 255, iterations: 1)
      PASS   : SpeedTest::test_ranges_utf8view()
      RESULT : SpeedTest::test_ranges_utf8view():
           10,882 nsecs per iteration (total: 10,882, iterations: 1)
           718 CPU cycles per iteration, 66 MHz (total: 718, iterations: 1)
           977 instructions per iteration, 1.361 instr/cycle (total: 977, iterations: 1)
           230 branch instructions per iteration, 21.1 M/sec (total: 230, iterations: 1)
      PASS   : SpeedTest::test_ranges_bytearrayview()
      RESULT : SpeedTest::test_ranges_bytearrayview():
           6,965 nsecs per iteration (total: 6,965, iterations: 1)
           655 CPU cycles per iteration, 94 MHz (total: 655, iterations: 1)
           977 instructions per iteration, 1.492 instr/cycle (total: 977, iterations: 1)
           230 branch instructions per iteration, 33 M/sec (total: 230, iterations: 1)
      PASS   : SpeedTest::cleanupTestCase()
      Totals: 5 passed, 0 failed, 0 skipped, 0 blacklisted, 73ms
      ********* Finished testing of SpeedTest *********
       

       

      #pragma GCC push_options
      #pragma GCC optimize("O2")
      
      #include <algorithm>
      
      #include <QTest>
      #include <QString>
      #include <QProcess>
      
      using namespace Qt::StringLiterals;
      using namespace std::string_view_literals;
      using namespace std::chrono_literals;
      
      const char *untranslatedText(const char *input)
      {
          QByteArray output = QByteArray("abcdefg……") + input;
          return output.constData();
      }
      
      class SpeedTest : public QObject {
          Q_OBJECT
      private Q_SLOTS:
          void initTestCase();
          void test_bytearray();
          void test_ranges_utf8view();
          void test_ranges_bytearrayview();
      private:
          bool result = false;
          QByteArray input;
      };
      
      QTEST_MAIN(SpeedTest)
      
      void SpeedTest::initTestCase()
      {
          QProcess p;
          p.start(u"echo"_s, {u"-n"_s, u"%1"_s});
          p.waitForFinished();
          input = p.readAllStandardOutput();
          QVERIFY(!input.isEmpty());
      }
      
      void SpeedTest::test_bytearray()
      {
          QBENCHMARK {
              result = QByteArrayView(untranslatedText(input)).contains(QByteArrayView("%1"));
          }
          QVERIFY(result);
      }
      
      void SpeedTest::test_ranges_utf8view()
      {
          QBENCHMARK {
              QUtf8StringView text{untranslatedText(input)};
              result = !std::ranges::search(text, QUtf8StringView("%1")).empty();
          }
          QVERIFY(result);
      }
      
      void SpeedTest::test_ranges_bytearrayview()
      {
          QBENCHMARK {
              QByteArrayView text(untranslatedText(input));
              result = !std::ranges::search(text, QByteArrayView("%1")).empty();
          }
          QVERIFY(result);
      }
      
      #include "main.moc"
      
      #pragma GCC pop_options
      
       

      Attachments

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

        Activity

          People

            thiago Thiago Macieira
            fusionfuture Yuanzheng Wang
            Votes:
            0 Vote for this issue
            Watchers:
            8 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes