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

QTextCursor.insertText() extremely slow

XMLWordPrintable

      Run the attached example with Qt 4.3.3 and click on the "Replace Test"
      button. The program does 10000 text replaces in 20000 lines of text.
      Under Qt 4.3.3 this takes about 11 seconds on a PC with 2.6GHz. I would
      expect this to work a lot faster!

      Doing the same with Qt 4.5 (snapshot 20090116) takes 205 seconds on
      the same machine - this is absolutely unusable!

      Commenting out the insertText line will bring run time from 200+ to 2 seconds.

      Example:

      #include <qapplication.h>
      #include <qdialog.h>
      #include <qlineedit.h>
      #include <qstring.h>
      #include <qtextcursor.h>
      #include <qtextdocument.h>
      #include <qtextedit.h>
      #include <qpushbutton.h>
      #include <qlayout.h>
      #include <stdio.h>
      #include <time.h>
      #include <QDebug>
      
      class cTextEditor : public QDialog {
        Q_OBJECT
      private:
        QTextEdit *edit;
        QPushButton *b;
      private slots:
        void ReplaceTest(void);
      public:
        cTextEditor(void);
        };
      
      cTextEditor::cTextEditor(void)
      {
        QVBoxLayout *vbl = new QVBoxLayout(this);
        edit = new QTextEdit;
        vbl->addWidget(edit);
        b = new QPushButton("Replace Test");
        vbl->addWidget(b);
        connect(b, SIGNAL(clicked(bool)), SLOT(ReplaceTest()));
        resize(400, 200);
        QString qs;
        for (int i = 10000; --i > 0; )
            qs += "Layer 1;\nRect R0 (5.25 -4.75) (5.75 -4.25);\n";
        edit->setPlainText(qs);
      }
      
      void cTextEditor::ReplaceTest(void)
      {
        QTextDocument *d = edit->document();
        if (d) {
           time_t t0 = time(NULL);
           QString FindText = "Layer 1;",
                   Replace  = "Layer 94;";
           setUpdatesEnabled(false);
           QTextCursor tc;
           b->setDisabled(true);
           for (;;) {
               tc = d->find(FindText, tc, QTextDocument::FindCaseSensitively);
               if (tc.isNull())
                  break;
               tc.insertText(Replace);
               edit->setTextCursor(tc);
      
               }
           b->setDisabled(false);
           setUpdatesEnabled(true);
           fprintf(stderr, "time = %ds\n", time(NULL) - t0);
           }
      }
      
      int main(int argc, char **argv)
      {
        QApplication a(argc, argv);
        QDialog *d = new cTextEditor;
        d->show();
        return a.exec();
      }
      
      #include "main.moc"
      

        1. QTBUG3554.zip
          1 kB
          Stefan Schweitzer
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            mitch_curtis Mitch Curtis
            janichol Andy Nichols
            Votes:
            16 Vote for this issue
            Watchers:
            21 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes