Skip to content

taskmanager: Use more efficient way to calculate the size of albumArtBackground

Fushan Wen requested to merge work/fusionfuture/efficient-crop into master

Before:

  1. width: 2/3 divisions, 1 comparison, 1/0 multiplication
  2. height: 3/2 divisions, 1 comparison,0/1 multiplication

After:

  1. scaleFactor: 2 divisions, 1 comparison
  2. 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

  1. new: 9202ms
  2. old: 12480ms
Edited by Fushan Wen

Merge request reports