Skip to main content



      Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » SWT performance question
SWT performance question [message #652039] Wed, 02 February 2011 08:56 Go to next message
Eclipse UserFriend
Hi, I'm trying to create a composite with a large number of child composites. The performance is ok, but much slower than the equivalent swing version. Is there a more efficient way to do this?

Thanks,

Greg

================
Swing version
================
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class JPanelExample {

	public static void main(String[] args) {

		int n = 50;

		JFrame frame = new JFrame();
		Container cp = frame.getContentPane();
		JPanel jpanel = new JPanel(new GridLayout(n, 1));
		cp.add(jpanel);

		long time = System.currentTimeMillis();

		for (int i = 0; i < n; ++i) {
			JPanel jpanel_1 = new JPanel(new GridLayout(1, n));
			jpanel.add(jpanel_1);
			for (int j = 0; j < n; ++j) {
				switch ((i * n + j) % 3) {
				case 0:
					JPanel inner = new JPanel();
					inner.setBackground(Color.red);
					jpanel_1.add(inner);
					break;
				case 1:
					JPanel inner2 = new JPanel();
					inner2.setBackground(Color.green);
					jpanel_1.add(inner2);
					break;
				case 2:
					JPanel inner3 = new JPanel();
					inner3.setBackground(Color.blue);
					jpanel_1.add(inner3);
					break;
				}
			}
		}

		System.out.println("time1=" + (System.currentTimeMillis() - time)); //$NON-NLS-1$

		frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		System.out.println("time2=" + (System.currentTimeMillis() - time)); //$NON-NLS-1$

	}
}

============
SWT version
============
package swtview;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Gridtest {

	public static void main(String[] args) {
		Display display = new Display();
		final Shell shell = new Shell(display);

		/**
		 * Here the number of elements in a row or column. Adjust this integer
		 * to increase the impact.
		 */
		int n = 50;
		shell.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
		GridLayout layout = new GridLayout(1, true);
		layout.horizontalSpacing = 0;
		layout.verticalSpacing = 0;
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		shell.setLayout(layout);

		long time = System.currentTimeMillis();

		for (int i = 0; i < n; i++) {
			Composite row = new Composite(shell, SWT.NONE);
			GridLayout layout1 = new GridLayout(n, true);
			layout1.horizontalSpacing = 0;
			layout1.verticalSpacing = 0;
			layout1.marginHeight = 0;
			layout1.marginWidth = 0;
			row.setLayout(layout1);
			GridData data = new GridData(SWT.LEFT, SWT.TOP, true, true);
			row.setLayoutData(data);
			for (int j = 0; j < n; j++) {
				GridData data1 = new GridData(SWT.LEFT, SWT.TOP, true, true);
				switch ((i * n + j) % 3) {
				case 0:
					Composite inner = new Composite(row, SWT.NONE);
					inner.setBackground(display.getSystemColor(SWT.COLOR_RED));
					inner.setLayoutData(data1);
					break;
				case 1:
					Composite inner2 = new Composite(row, SWT.NONE);
					inner2.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
					inner2.setLayoutData(data1);
					break;
				case 2:
					Composite inner3 = new Composite(row, SWT.NONE);
					inner3.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
					inner3.setLayoutData(data1);
					break;
				}

			}
		}

		System.out.println("time1=" + (System.currentTimeMillis() - time)); //$NON-NLS-1$

		shell.pack();
		shell.setSize(800, 600);
		shell.open();

		System.out.println("time2=" + (System.currentTimeMillis() - time)); //$NON-NLS-1$
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}

		display.dispose();
	}

}
Re: SWT performance question [message #652589 is a reply to message #652039] Fri, 04 February 2011 11:01 Go to previous message
Eclipse UserFriend
Hi Greg,

There's nothing wrong with the approach used in the snippet, but there are a
couple of changes that can be made to cut its time almost in half.
Hopefully these changes can be applied in the context of your real app as
well.

1. Since the Shell's size is set to 800x600 there's no reason to do
shell.pack(), since pack()'s calculated size is overridden by the subsequent
shell.setSize(...) call.

2. Surround the shell.open() invocation with setRedraw(false/true) like:

shell.setRedraw(false);
shell.open();
shell.setRedraw(true);

This is not something that should really be needed, and may indicate that
swt's implementation is missing some optimization opportunities when opening
a Shell and doing initial child layout. In the meantime though this
workaround should help.

Grant


"Greg Watson" <g.watson@computer.org> wrote in message
news:iibnf4$fvd$1@news.eclipse.org...
> Hi, I'm trying to create a composite with a large number of child
> composites. The performance is ok, but much slower than the equivalent
> swing version. Is there a more efficient way to do this?
>
> Thanks,
>
> Greg
>
> ================
> Swing version
> ================
>
> import java.awt.Color;
> import java.awt.Container;
> import java.awt.GridLayout;
>
> import javax.swing.JFrame;
> import javax.swing.JPanel;
>
> public class JPanelExample {
>
> public static void main(String[] args) {
>
> int n = 50;
>
> JFrame frame = new JFrame();
> Container cp = frame.getContentPane();
> JPanel jpanel = new JPanel(new GridLayout(n, 1));
> cp.add(jpanel);
>
> long time = System.currentTimeMillis();
>
> for (int i = 0; i < n; ++i) {
> JPanel jpanel_1 = new JPanel(new GridLayout(1, n));
> jpanel.add(jpanel_1);
> for (int j = 0; j < n; ++j) {
> switch ((i * n + j) % 3) {
> case 0:
> JPanel inner = new JPanel();
> inner.setBackground(Color.red);
> jpanel_1.add(inner);
> break;
> case 1:
> JPanel inner2 = new JPanel();
> inner2.setBackground(Color.green);
> jpanel_1.add(inner2);
> break;
> case 2:
> JPanel inner3 = new JPanel();
> inner3.setBackground(Color.blue);
> jpanel_1.add(inner3);
> break;
> }
> }
> }
>
> System.out.println("time1=" + (System.currentTimeMillis() - time));
> //$NON-NLS-1$
>
> frame.pack();
> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> frame.setVisible(true);
> System.out.println("time2=" + (System.currentTimeMillis() - time));
> //$NON-NLS-1$
>
> }
> }
>
> ============
> SWT version
> ============
>
> package swtview;
>
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.layout.GridData;
> import org.eclipse.swt.layout.GridLayout;
> import org.eclipse.swt.widgets.Composite;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Shell;
>
> public class Gridtest {
>
> public static void main(String[] args) {
> Display display = new Display();
> final Shell shell = new Shell(display);
>
> /**
> * Here the number of elements in a row or column. Adjust this integer
> * to increase the impact.
> */
> int n = 50;
> shell.setBackground(display.getSystemColor(SWT.COLOR_WHITE)) ;
> GridLayout layout = new GridLayout(1, true);
> layout.horizontalSpacing = 0;
> layout.verticalSpacing = 0;
> layout.marginHeight = 0;
> layout.marginWidth = 0;
> shell.setLayout(layout);
>
> long time = System.currentTimeMillis();
>
> for (int i = 0; i < n; i++) {
> Composite row = new Composite(shell, SWT.NONE);
> GridLayout layout1 = new GridLayout(n, true);
> layout1.horizontalSpacing = 0;
> layout1.verticalSpacing = 0;
> layout1.marginHeight = 0;
> layout1.marginWidth = 0;
> row.setLayout(layout1);
> GridData data = new GridData(SWT.LEFT, SWT.TOP, true, true);
> row.setLayoutData(data);
> for (int j = 0; j < n; j++) {
> GridData data1 = new GridData(SWT.LEFT, SWT.TOP, true, true);
> switch ((i * n + j) % 3) {
> case 0:
> Composite inner = new Composite(row, SWT.NONE);
> inner.setBackground(display.getSystemColor(SWT.COLOR_RED));
> inner.setLayoutData(data1);
> break;
> case 1:
> Composite inner2 = new Composite(row, SWT.NONE);
> inner2.setBackground(display.getSystemColor(SWT.COLOR_GREEN) );
> inner2.setLayoutData(data1);
> break;
> case 2:
> Composite inner3 = new Composite(row, SWT.NONE);
> inner3.setBackground(display.getSystemColor(SWT.COLOR_BLUE)) ;
> inner3.setLayoutData(data1);
> break;
> }
>
> }
> }
>
> System.out.println("time1=" + (System.currentTimeMillis() - time));
> //$NON-NLS-1$
>
> shell.pack();
> shell.setSize(800, 600);
> shell.open();
>
> System.out.println("time2=" + (System.currentTimeMillis() - time));
> //$NON-NLS-1$
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch()) {
> display.sleep();
> }
> }
>
> display.dispose();
> }
>
> }
>
Previous Topic:[SOLVED] Broken DND feedback indicator when dragging TableItems on Mac
Next Topic:Off-screen buffering?
Goto Forum:
  


Current Time: Sun Jul 06 08:08:00 EDT 2025

Powered by FUDForum. Page generated in 0.24465 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top