Performance improvement

Igor Mironchik igor.mironchik at gmail.com
Fri May 23 03:37:06 BST 2025


On 22.05.2025 22:16, Konstantin Kharlamov wrote:
> On Thu, 2025-05-22 at 19:21 +0300, Igor Mironchik wrote:
>> Hi.
>>
>> I'd like to ask you, guys, maybe I'm a dumb a little, and you know
>> how
>> to optimize a following function.
>>
>> inline long long int
>> skipIf(long long int startPos, const QString &line,
>>          const std::function<bool(const QChar &)> &pred,
>>          long long int endPos = -1)
>> {
>>       const auto length = (endPos < line.length() && endPos > -1 ?
>> endPos
>> : line.length());
>>
>>       while (startPos < length && pred(line[startPos])) {
>>           ++startPos;
>>       }
>>
>>       return startPos;
>> }
> Assuming the input is completely arbitrary and isn't in some way
> preprocessed (e.g. sorted or whatever), there's nothing to optimize.
> You have a O(n) algorithm, meaning you need to take up to n steps to
> find something, and your function directly implements such cycle. The
> function body is simple too, so with compiler optimizations enabled you
> should get the best possible machine code out of this.

I agree. Miracles don't happen in this world. But what if?! Thanks.



More information about the kde-devel mailing list