taskmanager: Use more efficient way to calculate the size of albumArtBackground
Before:
-
width
: 2/3 divisions, 1 comparison, 1/0 multiplication -
height
: 3/2 divisions, 1 comparison,0/1 multiplication
After:
-
scaleFactor
: 2 divisions, 1 comparison -
width/height
: no division or comparison,2 multiplications
Test
Source Code
import QtQuick 2.15
import QtQuick.Window 2.15
Window {
id: root
width: 640
height: 480
visible: false
title: qsTr("Hello World")
Component.onCompleted: {
let i = 0;
let scaleFactor, widt, heigh;
let w1 = 300, w2 = 100, h1 = 150, h2 = 100;
console.time("new");
for (i=0; i<100000000; i++) {
scaleFactor = Math.max(w1 / w2, h1 / h2);
widt = Math.round(w2 * scaleFactor);
heigh = Math.round(h2 * scaleFactor);
}
console.timeEnd("new");
console.time("old");
for (i=0; i<100000000; i++) {
widt = Math.round((w2 / h2) <= (w1 / h1) ? w1 : h1 * (w2 / h2));
heigh = Math.round((w2 / h2) <= (w1 / h1) ? w1 * (h2 / w2) : h1);
}
console.timeEnd("old");
}
}
Test Result
- new: 9202ms
- old: 12480ms
Edited by Fushan Wen