From 434c52269564c84e3ea57242b7aaa2fd7cad3f54 Mon Sep 17 00:00:00 2001 From: David Faure Date: Thu, 4 Aug 2016 10:01:21 +0200 Subject: Improve performance of QColor::name, now more than 4 times faster Before: HexRgb: 0.00230 ms per iteration, HexArgb: 0.00290 ms per iteration After: HexRgb: 0.00051 ms per iteration, HexArgb: 0.00061 ms per iteration This showed up as a relevant optimization when profiling KIconLoader which uses QColor::name() as part of the key -- thanks to Mark Gaiser for the investigation and first suggestion of a solution. I have also seen customer code writing a replacement for QColor::name() because it was too slow to be used as a hash key. Change-Id: I009ccdd712ea0d869d466e2c9894e0cea58f0e68 Reviewed-by: Marc Mutz --- src/gui/painting/qcolor.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/gui/painting') diff --git a/src/gui/painting/qcolor.cpp b/src/gui/painting/qcolor.cpp index 3f30c061dc..d0a60f3704 100644 --- a/src/gui/painting/qcolor.cpp +++ b/src/gui/painting/qcolor.cpp @@ -521,9 +521,10 @@ QString QColor::name(NameFormat format) const { switch (format) { case HexRgb: - return QString::asprintf("#%02x%02x%02x", red(), green(), blue()); + return QLatin1Char('#') + QString::number(rgba() | 0x1000000, 16).rightRef(6); case HexArgb: - return QString::asprintf("#%02x%02x%02x%02x", alpha(), red(), green(), blue()); + // it's called rgba() but it does return AARRGGBB + return QLatin1Char('#') + QString::number(rgba() | 0x100000000, 16).rightRef(8); } return QString(); } -- cgit v1.2.3