Congratulations to our 2022 Qt Champions!

File Searching operation taking more time using qt

  • Hi all,
    In my device, 500 files are in database.
    In our application, we search file from 500 files.
    Suppose same file name have upto 200 files ,we observed that searching operation consuming more time.
    Could you please let me know why its taking more time.


  • Hard to say with the information provided. More information would be needed:

    • Files are in a database. What kind of database?
    • Are there contents or perhaps just filenames?
    • How do you do the searching? Your Qt code?
    • Consumes more time compared with what other method?
    • How much more is "more time"? Millisecond? 10 percent? Ten times?
    • Your device, its characteristics, OS, Qt version?

  • I am using Linux and 4.4.3 qt version for arm processor.
    Processor frequency is 200Mhz.
    Following is code snippest:
    foreach(QString patientIdStr, m_patientList)
    if (patientIdStr.startsWith(ui->editSearch->text()))
    QString patientId = patientIdStr.section('_', 1, 1);

            testReportIds = CDatabaseManager::instance()->GetTestReportIdsWithPatient(patientId);
            if (!testReportIds.isEmpty())
                for (int index = 0; index < testReportIds.count(); index++)

    In this , its searches entered letter with existing files.
    Suppose 194 patient name which is start with letter "C" are present in database.
    The above code taking around 150 sec to serach patient Id for 194 patient from existing database.

    Can you explain how foreach works.
    Is any other alternative to make fast search.

  • Lifetime Qt Champion


    Not everything comes from that but your code is not the most efficient.

    For example:

    • patientIdStr, the way you wrote it, you create a copy at each iteration rather than using a const reference thus time and memory consuming.
    • your startsWith call, again, each iteration you retrieve the text from editSearch which is a waste of time, retrieve it once before starting the loop.
    • Why are you calling append m_testReportList.append once of each item in testReportIds ? You can do it in one call.
    • Why are you calling patientIds.append(patientIdStr); the same number of times you have testReportIds ?

  • Can u suggest how to break foreach loop

  • Lifetime Qt Champion

    What do you mean by "break foreach loop" ?

Log in to reply