I have a LogDisplay application. I want to reduce Time Complexity.
-
Hi,
I have a Log Display application which displays recent 10, 20 , 100 logs and use For loop to read these recent 100 lines. I want to reduce time complexity. Any suggestions on what function should I use?void LogDisplay::updateLogDisplay() { logDisplay->clear(); int count = 0; QTextCursor cursor(logDisplay->document()); cursor.movePosition(QTextCursor::End); for(int i = logList.size() - 1; i >= 0 && count < maxLines; --i) { QString log = logList[i]; if ((errorCheckbox->isChecked() && log.contains("ERROR")) || (warningCheckbox->isChecked() && log.contains("WARNING")) || (infoCheckbox->isChecked() && log.contains("INFO"))) { QTextCharFormat format; if (log.contains("ERROR")) { format.setForeground(Qt::red); } else if (log.contains("WARNING")) { format.setForeground(Qt::darkYellow); } cursor.insertText("\n"); cursor.movePosition(QTextCursor::Up); cursor.setCharFormat(format); cursor.insertText(log); ++count; } } } -
@Aviral-0 said in I have a LogDisplay application. I want to reduce Time Complexity.:
Error: No matching member function for call to 'filter'.
I do not see any overload of QStringList QStringList::filter() which takes a function/lambda argument, so I don't know where you got the idea from that it does.
Error: No member named 'toPlainTextFormat' in 'QTextOption'
I do not see any method of QTextOption named
toPlainTextFormat(), so I don't know where you got that from.@JonB Thankyou for reply!
Well I solved it:void LogDisplay::updateLogDisplay() { // Filter logList using lambda function QStringList filteredLogList; std::copy_if(logList.begin(), logList.end(), std::back_inserter(filteredLogList), [&](const QString& log) { if ((errorCheckbox->isChecked() && log.contains("ERROR")) || (warningCheckbox->isChecked() && log.contains("WARNING")) || (infoCheckbox->isChecked() && log.contains("INFO"))) { return true; } return false; }); // Check if the filtered log list is longer than maxLines if (filteredLogList.size() > maxLines) { // Remove oldest logs to limit the size to maxLines filteredLogList.erase(filteredLogList.begin(), filteredLogList.end() - maxLines); } QString logHtml; QTextCharFormat errorFormat, warningFormat, defaultFormat; errorFormat.setForeground(Qt::red); warningFormat.setForeground(Qt::darkYellow); QTextDocument document; document.setDefaultFont(logDisplay->font()); defaultFormat.setFont(document.defaultFont()); for (const QString& log : filteredLogList) { QTextCharFormat format = defaultFormat; if (log.contains("ERROR")) { format.merge(errorFormat); } else if (log.contains("WARNING")) { format.merge(warningFormat); } logHtml += QString("<p style=\"color:%1;\">%2</p>") .arg(format.foreground().color().name()) .arg(log.toHtmlEscaped()); } logDisplay->setPlainText(""); logDisplay->document()->setHtml(logHtml); } -
Hi,
I have a Log Display application which displays recent 10, 20 , 100 logs and use For loop to read these recent 100 lines. I want to reduce time complexity. Any suggestions on what function should I use?void LogDisplay::updateLogDisplay() { logDisplay->clear(); int count = 0; QTextCursor cursor(logDisplay->document()); cursor.movePosition(QTextCursor::End); for(int i = logList.size() - 1; i >= 0 && count < maxLines; --i) { QString log = logList[i]; if ((errorCheckbox->isChecked() && log.contains("ERROR")) || (warningCheckbox->isChecked() && log.contains("WARNING")) || (infoCheckbox->isChecked() && log.contains("INFO"))) { QTextCharFormat format; if (log.contains("ERROR")) { format.setForeground(Qt::red); } else if (log.contains("WARNING")) { format.setForeground(Qt::darkYellow); } cursor.insertText("\n"); cursor.movePosition(QTextCursor::Up); cursor.setCharFormat(format); cursor.insertText(log); ++count; } } } -
@Aviral-0
I guess you could build a single block of HTML, including the colors, for all lines and then just append the HTML in one go. Don't know if you'd notice the difference, and wouldn't expect what you already have to be a problem?@JonB I have tried this code:
void LogDisplay::updateLogDisplay() { QStringList filteredLogList = logList.filter([&](const QString& log) { if ((errorCheckbox->isChecked() && log.contains("ERROR")) || (warningCheckbox->isChecked() && log.contains("WARNING")) || (infoCheckbox->isChecked() && log.contains("INFO"))) { return true; } return false; }); QString logHtml; QTextCharFormat errorFormat, warningFormat, defaultFormat; errorFormat.setForeground(Qt::red); warningFormat.setForeground(Qt::darkYellow); QTextDocument document; document.setDefaultFont(logDisplay->font()); defaultFormat = document.defaultTextOption().toPlainTextFormat(); for (const QString& log : filteredLogList) { QTextCharFormat format = defaultFormat; if (log.contains("ERROR")) { format.merge(errorFormat); } else if (log.contains("WARNING")) { format.merge(warningFormat); } logHtml += QString("<p style=\"color:%1;\">%2</p>") .arg(format.foreground().color().name()) .arg(log.toHtmlEscaped()); } logDisplay->setPlainText(""); logDisplay->document()->setHtml(logHtml); }But It throws ERROR:
Error: No matching member function for call to 'filter'.
Error: No member named 'toPlainTextFormat' in 'QTextOption'
Please Tell me what to do. -
@JonB I have tried this code:
void LogDisplay::updateLogDisplay() { QStringList filteredLogList = logList.filter([&](const QString& log) { if ((errorCheckbox->isChecked() && log.contains("ERROR")) || (warningCheckbox->isChecked() && log.contains("WARNING")) || (infoCheckbox->isChecked() && log.contains("INFO"))) { return true; } return false; }); QString logHtml; QTextCharFormat errorFormat, warningFormat, defaultFormat; errorFormat.setForeground(Qt::red); warningFormat.setForeground(Qt::darkYellow); QTextDocument document; document.setDefaultFont(logDisplay->font()); defaultFormat = document.defaultTextOption().toPlainTextFormat(); for (const QString& log : filteredLogList) { QTextCharFormat format = defaultFormat; if (log.contains("ERROR")) { format.merge(errorFormat); } else if (log.contains("WARNING")) { format.merge(warningFormat); } logHtml += QString("<p style=\"color:%1;\">%2</p>") .arg(format.foreground().color().name()) .arg(log.toHtmlEscaped()); } logDisplay->setPlainText(""); logDisplay->document()->setHtml(logHtml); }But It throws ERROR:
Error: No matching member function for call to 'filter'.
Error: No member named 'toPlainTextFormat' in 'QTextOption'
Please Tell me what to do.@Aviral-0 said in I have a LogDisplay application. I want to reduce Time Complexity.:
Error: No matching member function for call to 'filter'.
I do not see any overload of QStringList QStringList::filter() which takes a function/lambda argument, so I don't know where you got the idea from that it does.
Error: No member named 'toPlainTextFormat' in 'QTextOption'
I do not see any method of QTextOption named
toPlainTextFormat(), so I don't know where you got that from. -
@Aviral-0 said in I have a LogDisplay application. I want to reduce Time Complexity.:
Error: No matching member function for call to 'filter'.
I do not see any overload of QStringList QStringList::filter() which takes a function/lambda argument, so I don't know where you got the idea from that it does.
Error: No member named 'toPlainTextFormat' in 'QTextOption'
I do not see any method of QTextOption named
toPlainTextFormat(), so I don't know where you got that from.@JonB Thankyou for reply!
Well I solved it:void LogDisplay::updateLogDisplay() { // Filter logList using lambda function QStringList filteredLogList; std::copy_if(logList.begin(), logList.end(), std::back_inserter(filteredLogList), [&](const QString& log) { if ((errorCheckbox->isChecked() && log.contains("ERROR")) || (warningCheckbox->isChecked() && log.contains("WARNING")) || (infoCheckbox->isChecked() && log.contains("INFO"))) { return true; } return false; }); // Check if the filtered log list is longer than maxLines if (filteredLogList.size() > maxLines) { // Remove oldest logs to limit the size to maxLines filteredLogList.erase(filteredLogList.begin(), filteredLogList.end() - maxLines); } QString logHtml; QTextCharFormat errorFormat, warningFormat, defaultFormat; errorFormat.setForeground(Qt::red); warningFormat.setForeground(Qt::darkYellow); QTextDocument document; document.setDefaultFont(logDisplay->font()); defaultFormat.setFont(document.defaultFont()); for (const QString& log : filteredLogList) { QTextCharFormat format = defaultFormat; if (log.contains("ERROR")) { format.merge(errorFormat); } else if (log.contains("WARNING")) { format.merge(warningFormat); } logHtml += QString("<p style=\"color:%1;\">%2</p>") .arg(format.foreground().color().name()) .arg(log.toHtmlEscaped()); } logDisplay->setPlainText(""); logDisplay->document()->setHtml(logHtml); } -
A Aviral 0 has marked this topic as solved on
-
@JonB Thankyou for reply!
Well I solved it:void LogDisplay::updateLogDisplay() { // Filter logList using lambda function QStringList filteredLogList; std::copy_if(logList.begin(), logList.end(), std::back_inserter(filteredLogList), [&](const QString& log) { if ((errorCheckbox->isChecked() && log.contains("ERROR")) || (warningCheckbox->isChecked() && log.contains("WARNING")) || (infoCheckbox->isChecked() && log.contains("INFO"))) { return true; } return false; }); // Check if the filtered log list is longer than maxLines if (filteredLogList.size() > maxLines) { // Remove oldest logs to limit the size to maxLines filteredLogList.erase(filteredLogList.begin(), filteredLogList.end() - maxLines); } QString logHtml; QTextCharFormat errorFormat, warningFormat, defaultFormat; errorFormat.setForeground(Qt::red); warningFormat.setForeground(Qt::darkYellow); QTextDocument document; document.setDefaultFont(logDisplay->font()); defaultFormat.setFont(document.defaultFont()); for (const QString& log : filteredLogList) { QTextCharFormat format = defaultFormat; if (log.contains("ERROR")) { format.merge(errorFormat); } else if (log.contains("WARNING")) { format.merge(warningFormat); } logHtml += QString("<p style=\"color:%1;\">%2</p>") .arg(format.foreground().color().name()) .arg(log.toHtmlEscaped()); } logDisplay->setPlainText(""); logDisplay->document()->setHtml(logHtml); } -
@JonB Thankyou for reply!
Well I solved it:void LogDisplay::updateLogDisplay() { // Filter logList using lambda function QStringList filteredLogList; std::copy_if(logList.begin(), logList.end(), std::back_inserter(filteredLogList), [&](const QString& log) { if ((errorCheckbox->isChecked() && log.contains("ERROR")) || (warningCheckbox->isChecked() && log.contains("WARNING")) || (infoCheckbox->isChecked() && log.contains("INFO"))) { return true; } return false; }); // Check if the filtered log list is longer than maxLines if (filteredLogList.size() > maxLines) { // Remove oldest logs to limit the size to maxLines filteredLogList.erase(filteredLogList.begin(), filteredLogList.end() - maxLines); } QString logHtml; QTextCharFormat errorFormat, warningFormat, defaultFormat; errorFormat.setForeground(Qt::red); warningFormat.setForeground(Qt::darkYellow); QTextDocument document; document.setDefaultFont(logDisplay->font()); defaultFormat.setFont(document.defaultFont()); for (const QString& log : filteredLogList) { QTextCharFormat format = defaultFormat; if (log.contains("ERROR")) { format.merge(errorFormat); } else if (log.contains("WARNING")) { format.merge(warningFormat); } logHtml += QString("<p style=\"color:%1;\">%2</p>") .arg(format.foreground().color().name()) .arg(log.toHtmlEscaped()); } logDisplay->setPlainText(""); logDisplay->document()->setHtml(logHtml); } -
@Aviral-0 said in I have a LogDisplay application. I want to reduce Time Complexity.:
Well I solved it:
So did doing it this way instead of inserting text item-by-item make a big difference to speed?
@JonB
only differen of thought In the QList there is method "reserve" which reserve the space. Max caposity is 100records.
If the loglist has many records in one single loop you can append records without realloc memory. Sorry this is only my thought which can be wrong. -
@JonB
only differen of thought In the QList there is method "reserve" which reserve the space. Max caposity is 100records.
If the loglist has many records in one single loop you can append records without realloc memory. Sorry this is only my thought which can be wrong.@piervalli
Your thought is not wrong, but it will make 0.00001% difference here :)
The only thing which could take any time is updating of the UI.