【问题标题】:Performance for enabling/disabling of Swing Components启用/禁用 Swing 组件的性能
【发布时间】:2012-06-06 11:22:06
【问题描述】:

我正在开发一个包含很多组件的大型(至少对我而言)gui。启用规则背后有一些相当复杂的机制(意味着必须根据其他组件的状态启用/禁用哪个组件)。每次我更新整个用户界面时(这并不经常但有时是必要的,例如在加载设置文件之后),我必须通过规则集来设置每个组件。我基本上做的是以下几点:

meaningfulName1.setEnabled(conditionForMeaningfulName1());
meaningfulName2.setEnabled(conditionForMeaningfulName2());
meaningfulName3.setEnabled(conditionForMeaningfulName3());
// etc

我问自己这样做是否有意义。

boolean temp = conditionForMeaningfulName1();
if (meaningfulName1.isEnabled != temp) meaningfulName1.setEnabled(temp);
temp = conditionForMeaningfulName2();
if (meaningfulName2.isEnabled != temp) meaningfulName2.setEnabled(temp);
temp = conditionForMeaningfulName3();
if (meaningfulName3.isEnabled != temp) meaningfulName3.setEnabled(temp);
// etc

其背后的想法是,如果状态已经是所需的状态,它可以节省一些性能而不设置标志,从而节省一些图形更新(以及函数调用)。

您认为这在大规模上有意义还是根本没有节省任何时间并且只会降低代码的可读性?

【问题讨论】:

  • 好吧,我突然想到,我可以在 JComponent.java 中查找它,实际上图形更新repaint(); 仅在状态更改时才会触发。所以我想不会有任何加速。如果有人对此有异议,请发表答案,否则我将在几天后自己做出答复。
  • “它根本不会节省任何时间” 我怀疑不会,但最好用分析代替怀疑。

标签: java performance swing


【解决方案1】:

根据 Andrew Thompson 的评论,我创建了一个小测试。我没有使用分析,只是很简单System.currentTimeMillis()。显然前几次运行必须忽略,但之后这两种方法似乎没有区别。

import java.awt.GridLayout;
import java.util.Random;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Main {
    public static Vector<int[]> v;
    public static JButton[][] buttons;
    public static int n1 = 100, n2 = 100, N = 50000;

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setLayout(new GridLayout(n1, n2));

        buttons = new JButton[n1][n2];
        for (int i = 0; i < n1; i++) {
            for (int j = 0; j < n2; j++) {
                buttons[i][j] = new JButton(String.format("%d,%d", i, j));
                f.add(buttons[i][j]);
            }
        }
        f.setSize(1024, 768);
        f.setLocationRelativeTo(null);
        f.setVisible(true);

        v = new Vector<int[]>();
        Random r = new Random();
        for (int i = 0; i < N; i++)
            v.add(new int[] {r.nextInt(n1), r.nextInt(n2), r.nextInt(2)});

        for (int i = 0; i < 10; i++) {
            test1();
            test2();
        }
    }

    public static void test1() {
        reset();
        new Thread(new Runnable() {
            @Override
            public void run() {
                long start = System.currentTimeMillis();
                for (int[] test : v) {
                    buttons[test[0]][test[1]].setEnabled(test[2] == 1);
                }
                System.out.println(String.format("Test1   -> took %dms", System.currentTimeMillis() - start));
            }
        }).start();
    }

    public static void test2() {
        reset();
        new Thread(new Runnable() {
            @Override
            public void run() {
                long start = System.currentTimeMillis();
                for (int[] test : v) {
                    if (buttons[test[0]][test[1]].isEnabled() != (test[2] == 1)) buttons[test[0]][test[1]].setEnabled(test[2] == 1);
                }
                System.out.println(String.format("Test2   -> took %dms", System.currentTimeMillis() - start));
            }
        }).start();
    }

    public static void reset() {
        for (int i = 0; i < n1; i++) {
            for (int j = 0; j < n2; j++) {
                buttons[i][j].setEnabled(true);
            }
        }
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {}
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 2012-12-04
    • 2013-06-28
    • 2014-10-05
    相关资源
    最近更新 更多