1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
/*******************************************************************************
* Copyright (c) 2018 Red Hat and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat - initial API and implementation
*******************************************************************************/
package org.eclipse.swt.tests.gtk.snippets;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
/*
* Title: Adding items in combo box grows at O(n^2).
* how to run: Run on Gtk2/3 with/without fix to observe different run times.
* Gtk2, +-20 ms
* Gtk3, +- 1000ms
* Gtk3 with fix: += 10ms.
*
* Once snippet launched, try 'refill' button, which tests time it takes to remove entries.
*
*/
public class Bug489640_ComboPerformanceTest {
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
test_setItems(shell);
test_addItems(shell);
shell.setSize(400, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
private static void test_addItems(final Shell shell) {
final Combo combo = new Combo(shell, SWT.BORDER | SWT.READ_ONLY);
combo.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
final long start = System.currentTimeMillis();
int count = 200;
for (int i = 0; i < count; i++) {
combo.add("item " + i); // <<< via add.
}
System.out.println("Combo.add(..) took " + (System.currentTimeMillis() - start) + " ms. (1000ms = bad. 20ms = good.)");
combo.select(0);
}
private static void test_setItems(final Shell shell) {
final Combo combo = new Combo(shell, SWT.BORDER | SWT.READ_ONLY);
combo.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
int count = 200;
final String[] items = new String[count];
for (int i = 0; i < count; i++) {
items[i] = new String("item " + i);
}
final long start = System.currentTimeMillis();
combo.setItems(items);
System.out.println("Combo.setItems(.) took " + (System.currentTimeMillis() - start) + " ms. (1000ms = bad. 20ms = good.)");
combo.select(0);
final Button button = new Button(shell, SWT.PUSH);
button.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false));
button.setText("Refill Combo at runtime");
button.addListener(SWT.Selection, event -> {
final Map<String, Charset> charSets = Charset.availableCharsets();
final List<String> encodings = new ArrayList<>(charSets.keySet());
Collections.sort(encodings);
final String[] items1 = encodings.toArray(new String[encodings.size()]);
System.out.println("Setting widget.combo with " + items1.length + " items");
final long start1 = System.currentTimeMillis();
combo.setItems(items1);
final long end = System.currentTimeMillis();
System.out.println("took " + (end - start1) + " ms.");
combo.select(0);
});
}
}
|