[Development] QStringLiteral vs QLatin1String , foreach vs for range

Thiago Macieira thiago.macieira at intel.com
Mon Jan 18 00:28:13 CET 2016

On Sunday 17 January 2016 22:22:39 Gunnar Roth wrote:
> why is QLatin1String more efficient than QLiteralString in this case? both
> strings  being uft16 seems to be faster for, you could use size_t chunks
> for comparison for example.

Your premise is wrong. The QLatin1String is not stored as UTF-16. It's stored 
as Latin1.

If you're asking about runtime performance, it's because we have algorithms to 
perform the operation without converting in the common case scenario.

Take the equality operator and QString::compare: sure it's more efficient to do 
binary compares of two QChars, especially if you can unroll the loop and do 
SIMD (like we do, see [1]). But if your initial data is Latin1, you'd need to 
incur a performance penalty to allocate memory and then convert from Latin1 to 
UTF-16 before doing the comparison. So QString has a UTF-16-to-Latin1 
comparison, which is quite efficient (and also SIMD-enabled, see [2]).

Maybe in some cases the UTF16-to-Latin1 operation is slower than pure UTF-16, 
but is almost always faster than the combined malloc+convert+operate+free. In 
this particular case, the ucstrncmp for uchar is either as fast as the QChar 
one, or in the case of AVX2, possibly even faster in longer strings (less 
memory being read).

Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center

More information about the Development mailing list