QTimeZone::utc(): micro-optimize it

QTimeZone::utc() goes through the QTimeZone(int) constructor,
passing 0 to it. That in turn calls the QUtcTimeZonePrivate(int)
constructor, which ends up matching the zero offset and doing some
string comparisons.

I think we can bypass this mechanism and simply have QTimeZone::utc()
create a QUtcTimeZonePrivate and set it.

Compare the following results for tst_QTimeZone::utc benchmark as
built on a machine with:

    Config: Using QtTest library 6.10.0, Qt 6.10.0 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 14.2.1 20250207), arch unknown

Before:

    36.708812 nsecs per iteration (total: 36,708,776, iterations: 999999)
    169.024518 CPU cycles per iteration, 4,6 GHz (total: 169,024,349, iterations: 999999)
    601.286607 instructions per iteration, 3,557 instr/cycle (total: 601,286,006, iterations: 999999)
    139.051610 branch instructions per iteration, 3,79 G/sec (total: 139,051,471, iterations: 999999)

After:

    29.204813 nsecs per iteration (total: 29,204,784, iterations: 999999)
    129.981345 CPU cycles per iteration, 4,45 GHz (total: 129,981,216, iterations: 999999)
    408.260941 instructions per iteration, 3,141 instr/cycle (total: 408,260,533, iterations: 999999)
    85.047524 branch instructions per iteration, 2,91 G/sec (total: 85,047,439, iterations: 999999)

Relative Delta:

            before   after    improvement
    nsecs    36.7     29.2    1.26x
    cycles  169.0    130.0    1.30x
    instr.  601.3    408.0    1.47x
    branch  139.1     85.0    1.63x

Change-Id: Ic6fe605a3c6a83096315e2cb4309b9f67e83bccc
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Milian Wolff <milian.wolff@kdab.com>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Giuseppe D'Angelo 2025-02-22 13:40:11 +01:00 committed by Marc Mutz
parent fcde54148d
commit b3c75be5e0

View File

@ -1455,7 +1455,7 @@ QTimeZone QTimeZone::systemTimeZone()
*/
QTimeZone QTimeZone::utc()
{
return QTimeZone(0);
return QTimeZone(*new QUtcTimeZonePrivate());
}
/*!