Home » Eclipse Projects » Standard Widget Toolkit (SWT) » SWT performance on drawing text (compare with Swing, sample code provided)
SWT performance on drawing text (compare with Swing, sample code provided) [message #846702] |
Mon, 16 April 2012 10:03 |
Leung Wang Hei Messages: 64 Registered: July 2010 |
Member |
|
|
All,
A classic question but it really surprise me for the difference in numbers.
Attached simple tests in Swing and SWT, drawing text with random fonts
1000 times.
SWT (gc.advance off)
~80ms for every 1000 text drawn
SWT (gc.advance on)
~200ms for every 1000 text drawn
Swing (with Anti-aliasing hints on)
~30ms for every 1000 text drawn
Config:
Win7
Java 1.6_25
SWT org.eclipse.swt.win32.win32.x86_3.7.1.v3738a.jar
Thanks for you all.
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Drawable;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class SWTTest {
public static void main(String[] args) throws IOException {
Display display = new Display ();
Shell shell = new Shell(display);
shell.setBounds(0, 0, 500, 500);
shell.open();
drawshit(shell);
while (!shell.isDisposed ()) {
if (display.readAndDispatch ())
drawshit(shell);
else
display.sleep ();
}
display.dispose ();
//AWT
AWTTest test = new AWTTest();
System.in.read();
}
private static void drawshit(Shell shell) {
if(!shell.isDisposed())
{
GC gc = new GC(shell);
Rectangle bounds = shell.getBounds();
long start = System.nanoTime();
for(int i=0;i<1000;i++)
{
//try various different things here, e.g. drawing lines, rectangles, cacheing fonts etc.
Font font = new Font(gc.getDevice(), new FontData("Tahoma", (int) (Math.random()*30), SWT.NORMAL));
gc.setFont(font);
gc.drawText("Hello World", (int) (Math.random()*400), (int)(Math.random()*400));
font.dispose();
}
long stop = System.nanoTime();
gc.dispose();
System.out.println("SWT time: " + (stop-start)/1000000 + " ms" );
}
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Label;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class AWTTest extends Frame implements MouseListener, WindowListener {
public static void main(String[] args) {
new AWTTest();
}
public AWTTest() {
/*
Label hello = new Label("Hello World");
add(hello, "Center");
*/
setSize(500,500);
setVisible(true);
addMouseListener(this);
addWindowListener(this);
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
//SWT Default font:
//[1|Tahoma|8.25|0|WINDOWS|1|-11|0|0|0|400|0|0|0|1|0|0|0|0|Tahoma]
g2d.setBackground(Color.GRAY);
g2d.clearRect(0, 0, 500, 500);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
long start = System.nanoTime();
for(int i=0;i<1000;i++)
{
//try various different things here, e.g. drawing lines, rectangles, cacheing fonts etc.
g2d.setFont(new Font("Tahoma", Font.PLAIN, (int) (Math.random()*30)));
g2d.drawString("Hello World", (int) (Math.random()*400), (int)(Math.random()*400));
}
long stop = System.nanoTime();
System.out.println("AWT time: " + (stop-start)/1000000 + " ms" );
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
this.paintAll(getGraphics());
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
}
-
Attachment: SWTTest.java
(Size: 1.53KB, Downloaded 195 times) -
Attachment: AWTTest.java
(Size: 2.40KB, Downloaded 175 times)
|
|
| | |
Re: SWT performance on drawing text (compare with Swing, sample code provided) [message #847507 is a reply to message #847216] |
Tue, 17 April 2012 11:29 |
Leung Wang Hei Messages: 64 Registered: July 2010 |
Member |
|
|
Like you said, I moved font creation/disposal out of the drawing loop.
I am able to lower the SWT time from 80ms to 60ms (25%-)
but still AWT gives me less than 30ms.
What am I missing here?
Ludwig, should your middle "AWT time: 137 ms" be "SWT time: 137 ms" ?
On 4/17/2012 1:18 PM, Ludwig Moser wrote:
> i changed the SWT code to proper useage of the fonts (create fonts
> before doing the test and disposing it afterwards.
> results here are:
>
> SWT time: 74 ms
> AWT time: 137 ms
> AWT time: 75 ms
>
> note that i have a crappy old "Pentium D" cpu ;)
package SWT.vs.J2D;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SWTTest {
private static Font font;
public static void main(String[] args) throws IOException {
Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(0, 0, 500, 500);
shell.open();
font = new Font(display, new FontData("Tahoma", (int) (Math.random() * 30), SWT.NORMAL));
shell.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
drawshit(e.gc);
}
});
// drawshit(shell);
while (!shell.isDisposed()) {
if (display.readAndDispatch()) {
}
// drawshit(shell);
else {
display.sleep();
}
}
font.dispose();
display.dispose();
// AWT
AWTTest test = new AWTTest();
System.in.read();
}
// private static void drawshit(Shell shell) {
private static void drawshit(GC gc) {
// if (!shell.isDisposed()) {
// GC gc = new GC(shell);
// Rectangle bounds = shell.getBounds();
long start = System.nanoTime();
for (int i = 0; i < 1000; i++) {
// try various different things here, e.g. drawing lines, rectangles, cacheing fonts etc.
gc.setFont(font);
gc.drawText("Hello World", (int) (Math.random() * 400), (int) (Math.random() * 400));
}
long stop = System.nanoTime();
// gc.dispose();
System.out.println("SWT time: " + (stop - start) / 1000000 + " ms");
// }
}
}
-
Attachment: SWTTest.java
(Size: 1.73KB, Downloaded 177 times)
|
|
|
Re: SWT performance on drawing text (compare with Swing, sample code provided) [message #847511 is a reply to message #847216] |
Tue, 17 April 2012 11:29 |
Leung Wang Hei Messages: 64 Registered: July 2010 |
Member |
|
|
Like you said, I moved font creation/disposal out of the drawing loop.
I am able to lower the SWT time from 80ms to 60ms (25%-)
but still AWT gives me less than 30ms.
What am I missing here?
Ludwig, should your middle "AWT time: 137 ms" be "SWT time: 137 ms" ?
On 4/17/2012 1:18 PM, Ludwig Moser wrote:
> i changed the SWT code to proper useage of the fonts (create fonts
> before doing the test and disposing it afterwards.
> results here are:
>
> SWT time: 74 ms
> AWT time: 137 ms
> AWT time: 75 ms
>
> note that i have a crappy old "Pentium D" cpu ;)
package SWT.vs.J2D;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SWTTest {
private static Font font;
public static void main(String[] args) throws IOException {
Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(0, 0, 500, 500);
shell.open();
font = new Font(display, new FontData("Tahoma", (int) (Math.random() * 30), SWT.NORMAL));
shell.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
drawshit(e.gc);
}
});
// drawshit(shell);
while (!shell.isDisposed()) {
if (display.readAndDispatch()) {
}
// drawshit(shell);
else {
display.sleep();
}
}
font.dispose();
display.dispose();
// AWT
AWTTest test = new AWTTest();
System.in.read();
}
// private static void drawshit(Shell shell) {
private static void drawshit(GC gc) {
// if (!shell.isDisposed()) {
// GC gc = new GC(shell);
// Rectangle bounds = shell.getBounds();
long start = System.nanoTime();
for (int i = 0; i < 1000; i++) {
// try various different things here, e.g. drawing lines, rectangles, cacheing fonts etc.
gc.setFont(font);
gc.drawText("Hello World", (int) (Math.random() * 400), (int) (Math.random() * 400));
}
long stop = System.nanoTime();
// gc.dispose();
System.out.println("SWT time: " + (stop - start) / 1000000 + " ms");
// }
}
}
-
Attachment: SWTTest.java
(Size: 1.73KB, Downloaded 165 times)
|
|
|
Re: SWT performance on drawing text (compare with Swing, sample code provided) [message #847515 is a reply to message #847216] |
Tue, 17 April 2012 11:29 |
Leung Wang Hei Messages: 64 Registered: July 2010 |
Member |
|
|
Like you said, I moved font creation/disposal out of the drawing loop.
I am able to lower the SWT time from 80ms to 60ms (25%-)
but still AWT gives me less than 30ms.
What am I missing here?
Ludwig, should your middle "AWT time: 137 ms" be "SWT time: 137 ms" ?
On 4/17/2012 1:18 PM, Ludwig Moser wrote:
> i changed the SWT code to proper useage of the fonts (create fonts
> before doing the test and disposing it afterwards.
> results here are:
>
> SWT time: 74 ms
> AWT time: 137 ms
> AWT time: 75 ms
>
> note that i have a crappy old "Pentium D" cpu ;)
package SWT.vs.J2D;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SWTTest {
private static Font font;
public static void main(String[] args) throws IOException {
Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(0, 0, 500, 500);
shell.open();
font = new Font(display, new FontData("Tahoma", (int) (Math.random() * 30), SWT.NORMAL));
shell.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
drawshit(e.gc);
}
});
// drawshit(shell);
while (!shell.isDisposed()) {
if (display.readAndDispatch()) {
}
// drawshit(shell);
else {
display.sleep();
}
}
font.dispose();
display.dispose();
// AWT
AWTTest test = new AWTTest();
System.in.read();
}
// private static void drawshit(Shell shell) {
private static void drawshit(GC gc) {
// if (!shell.isDisposed()) {
// GC gc = new GC(shell);
// Rectangle bounds = shell.getBounds();
long start = System.nanoTime();
for (int i = 0; i < 1000; i++) {
// try various different things here, e.g. drawing lines, rectangles, cacheing fonts etc.
gc.setFont(font);
gc.drawText("Hello World", (int) (Math.random() * 400), (int) (Math.random() * 400));
}
long stop = System.nanoTime();
// gc.dispose();
System.out.println("SWT time: " + (stop - start) / 1000000 + " ms");
// }
}
}
-
Attachment: SWTTest.java
(Size: 1.73KB, Downloaded 189 times)
|
|
|
Re: SWT performance on drawing text (compare with Swing, sample code provided) [message #847520 is a reply to message #847216] |
Tue, 17 April 2012 11:29 |
Leung Wang Hei Messages: 64 Registered: July 2010 |
Member |
|
|
Like you said, I moved font creation/disposal out of the drawing loop.
I am able to lower the SWT time from 80ms to 60ms (25%-)
but still AWT gives me less than 30ms.
What am I missing here?
Ludwig, should your middle "AWT time: 137 ms" be "SWT time: 137 ms" ?
On 4/17/2012 1:18 PM, Ludwig Moser wrote:
> i changed the SWT code to proper useage of the fonts (create fonts
> before doing the test and disposing it afterwards.
> results here are:
>
> SWT time: 74 ms
> AWT time: 137 ms
> AWT time: 75 ms
>
> note that i have a crappy old "Pentium D" cpu ;)
package SWT.vs.J2D;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SWTTest {
private static Font font;
public static void main(String[] args) throws IOException {
Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(0, 0, 500, 500);
shell.open();
font = new Font(display, new FontData("Tahoma", (int) (Math.random() * 30), SWT.NORMAL));
shell.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
drawshit(e.gc);
}
});
// drawshit(shell);
while (!shell.isDisposed()) {
if (display.readAndDispatch()) {
}
// drawshit(shell);
else {
display.sleep();
}
}
font.dispose();
display.dispose();
// AWT
AWTTest test = new AWTTest();
System.in.read();
}
// private static void drawshit(Shell shell) {
private static void drawshit(GC gc) {
// if (!shell.isDisposed()) {
// GC gc = new GC(shell);
// Rectangle bounds = shell.getBounds();
long start = System.nanoTime();
for (int i = 0; i < 1000; i++) {
// try various different things here, e.g. drawing lines, rectangles, cacheing fonts etc.
gc.setFont(font);
gc.drawText("Hello World", (int) (Math.random() * 400), (int) (Math.random() * 400));
}
long stop = System.nanoTime();
// gc.dispose();
System.out.println("SWT time: " + (stop - start) / 1000000 + " ms");
// }
}
}
-
Attachment: SWTTest.java
(Size: 1.73KB, Downloaded 257 times)
|
|
|
Re: SWT performance on drawing text (compare with Swing, sample code provided) [message #847525 is a reply to message #847216] |
Tue, 17 April 2012 11:29 |
Leung Wang Hei Messages: 64 Registered: July 2010 |
Member |
|
|
Like you said, I moved font creation/disposal out of the drawing loop.
I am able to lower the SWT time from 80ms to 60ms (25%-)
but still AWT gives me less than 30ms.
What am I missing here?
Ludwig, should your middle "AWT time: 137 ms" be "SWT time: 137 ms" ?
On 4/17/2012 1:18 PM, Ludwig Moser wrote:
> i changed the SWT code to proper useage of the fonts (create fonts
> before doing the test and disposing it afterwards.
> results here are:
>
> SWT time: 74 ms
> AWT time: 137 ms
> AWT time: 75 ms
>
> note that i have a crappy old "Pentium D" cpu ;)
package SWT.vs.J2D;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SWTTest {
private static Font font;
public static void main(String[] args) throws IOException {
Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(0, 0, 500, 500);
shell.open();
font = new Font(display, new FontData("Tahoma", (int) (Math.random() * 30), SWT.NORMAL));
shell.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
drawshit(e.gc);
}
});
// drawshit(shell);
while (!shell.isDisposed()) {
if (display.readAndDispatch()) {
}
// drawshit(shell);
else {
display.sleep();
}
}
font.dispose();
display.dispose();
// AWT
AWTTest test = new AWTTest();
System.in.read();
}
// private static void drawshit(Shell shell) {
private static void drawshit(GC gc) {
// if (!shell.isDisposed()) {
// GC gc = new GC(shell);
// Rectangle bounds = shell.getBounds();
long start = System.nanoTime();
for (int i = 0; i < 1000; i++) {
// try various different things here, e.g. drawing lines, rectangles, cacheing fonts etc.
gc.setFont(font);
gc.drawText("Hello World", (int) (Math.random() * 400), (int) (Math.random() * 400));
}
long stop = System.nanoTime();
// gc.dispose();
System.out.println("SWT time: " + (stop - start) / 1000000 + " ms");
// }
}
}
-
Attachment: SWTTest.java
(Size: 1.73KB, Downloaded 189 times)
|
|
|
Re: SWT performance on drawing text (compare with Swing, sample code provided) [message #847529 is a reply to message #847216] |
Tue, 17 April 2012 11:29 |
Leung Wang Hei Messages: 64 Registered: July 2010 |
Member |
|
|
Like you said, I moved font creation/disposal out of the drawing loop.
I am able to lower the SWT time from 80ms to 60ms (25%-)
but still AWT gives me less than 30ms.
What am I missing here?
Ludwig, should your middle "AWT time: 137 ms" be "SWT time: 137 ms" ?
On 4/17/2012 1:18 PM, Ludwig Moser wrote:
> i changed the SWT code to proper useage of the fonts (create fonts
> before doing the test and disposing it afterwards.
> results here are:
>
> SWT time: 74 ms
> AWT time: 137 ms
> AWT time: 75 ms
>
> note that i have a crappy old "Pentium D" cpu ;)
package SWT.vs.J2D;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SWTTest {
private static Font font;
public static void main(String[] args) throws IOException {
Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(0, 0, 500, 500);
shell.open();
font = new Font(display, new FontData("Tahoma", (int) (Math.random() * 30), SWT.NORMAL));
shell.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
drawshit(e.gc);
}
});
// drawshit(shell);
while (!shell.isDisposed()) {
if (display.readAndDispatch()) {
}
// drawshit(shell);
else {
display.sleep();
}
}
font.dispose();
display.dispose();
// AWT
AWTTest test = new AWTTest();
System.in.read();
}
// private static void drawshit(Shell shell) {
private static void drawshit(GC gc) {
// if (!shell.isDisposed()) {
// GC gc = new GC(shell);
// Rectangle bounds = shell.getBounds();
long start = System.nanoTime();
for (int i = 0; i < 1000; i++) {
// try various different things here, e.g. drawing lines, rectangles, cacheing fonts etc.
gc.setFont(font);
gc.drawText("Hello World", (int) (Math.random() * 400), (int) (Math.random() * 400));
}
long stop = System.nanoTime();
// gc.dispose();
System.out.println("SWT time: " + (stop - start) / 1000000 + " ms");
// }
}
}
-
Attachment: SWTTest.java
(Size: 1.73KB, Downloaded 199 times)
|
|
|
Re: SWT performance on drawing text (compare with Swing, sample code provided) [message #847533 is a reply to message #847216] |
Tue, 17 April 2012 11:29 |
Leung Wang Hei Messages: 64 Registered: July 2010 |
Member |
|
|
Like you said, I moved font creation/disposal out of the drawing loop.
I am able to lower the SWT time from 80ms to 60ms (25%-)
but still AWT gives me less than 30ms.
What am I missing here?
Ludwig, should your middle "AWT time: 137 ms" be "SWT time: 137 ms" ?
On 4/17/2012 1:18 PM, Ludwig Moser wrote:
> i changed the SWT code to proper useage of the fonts (create fonts
> before doing the test and disposing it afterwards.
> results here are:
>
> SWT time: 74 ms
> AWT time: 137 ms
> AWT time: 75 ms
>
> note that i have a crappy old "Pentium D" cpu ;)
package SWT.vs.J2D;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SWTTest {
private static Font font;
public static void main(String[] args) throws IOException {
Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(0, 0, 500, 500);
shell.open();
font = new Font(display, new FontData("Tahoma", (int) (Math.random() * 30), SWT.NORMAL));
shell.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
drawshit(e.gc);
}
});
// drawshit(shell);
while (!shell.isDisposed()) {
if (display.readAndDispatch()) {
}
// drawshit(shell);
else {
display.sleep();
}
}
font.dispose();
display.dispose();
// AWT
AWTTest test = new AWTTest();
System.in.read();
}
// private static void drawshit(Shell shell) {
private static void drawshit(GC gc) {
// if (!shell.isDisposed()) {
// GC gc = new GC(shell);
// Rectangle bounds = shell.getBounds();
long start = System.nanoTime();
for (int i = 0; i < 1000; i++) {
// try various different things here, e.g. drawing lines, rectangles, cacheing fonts etc.
gc.setFont(font);
gc.drawText("Hello World", (int) (Math.random() * 400), (int) (Math.random() * 400));
}
long stop = System.nanoTime();
// gc.dispose();
System.out.println("SWT time: " + (stop - start) / 1000000 + " ms");
// }
}
}
-
Attachment: SWTTest.java
(Size: 1.73KB, Downloaded 175 times)
|
|
|
Re: SWT performance on drawing text (compare with Swing, sample code provided) [message #847537 is a reply to message #847216] |
Tue, 17 April 2012 11:29 |
Leung Wang Hei Messages: 64 Registered: July 2010 |
Member |
|
|
Like you said, I moved font creation/disposal out of the drawing loop.
I am able to lower the SWT time from 80ms to 60ms (25%-)
but still AWT gives me less than 30ms.
What am I missing here?
Ludwig, should your middle "AWT time: 137 ms" be "SWT time: 137 ms" ?
On 4/17/2012 1:18 PM, Ludwig Moser wrote:
> i changed the SWT code to proper useage of the fonts (create fonts
> before doing the test and disposing it afterwards.
> results here are:
>
> SWT time: 74 ms
> AWT time: 137 ms
> AWT time: 75 ms
>
> note that i have a crappy old "Pentium D" cpu ;)
package SWT.vs.J2D;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SWTTest {
private static Font font;
public static void main(String[] args) throws IOException {
Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(0, 0, 500, 500);
shell.open();
font = new Font(display, new FontData("Tahoma", (int) (Math.random() * 30), SWT.NORMAL));
shell.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
drawshit(e.gc);
}
});
// drawshit(shell);
while (!shell.isDisposed()) {
if (display.readAndDispatch()) {
}
// drawshit(shell);
else {
display.sleep();
}
}
font.dispose();
display.dispose();
// AWT
AWTTest test = new AWTTest();
System.in.read();
}
// private static void drawshit(Shell shell) {
private static void drawshit(GC gc) {
// if (!shell.isDisposed()) {
// GC gc = new GC(shell);
// Rectangle bounds = shell.getBounds();
long start = System.nanoTime();
for (int i = 0; i < 1000; i++) {
// try various different things here, e.g. drawing lines, rectangles, cacheing fonts etc.
gc.setFont(font);
gc.drawText("Hello World", (int) (Math.random() * 400), (int) (Math.random() * 400));
}
long stop = System.nanoTime();
// gc.dispose();
System.out.println("SWT time: " + (stop - start) / 1000000 + " ms");
// }
}
}
-
Attachment: SWTTest.java
(Size: 1.73KB, Downloaded 202 times)
|
|
|
Re: SWT performance on drawing text (compare with Swing, sample code provided) [message #847541 is a reply to message #847216] |
Tue, 17 April 2012 11:29 |
Leung Wang Hei Messages: 64 Registered: July 2010 |
Member |
|
|
Like you said, I moved font creation/disposal out of the drawing loop.
I am able to lower the SWT time from 80ms to 60ms (25%-)
but still AWT gives me less than 30ms.
What am I missing here?
Ludwig, should your middle "AWT time: 137 ms" be "SWT time: 137 ms" ?
On 4/17/2012 1:18 PM, Ludwig Moser wrote:
> i changed the SWT code to proper useage of the fonts (create fonts
> before doing the test and disposing it afterwards.
> results here are:
>
> SWT time: 74 ms
> AWT time: 137 ms
> AWT time: 75 ms
>
> note that i have a crappy old "Pentium D" cpu ;)
package SWT.vs.J2D;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SWTTest {
private static Font font;
public static void main(String[] args) throws IOException {
Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(0, 0, 500, 500);
shell.open();
font = new Font(display, new FontData("Tahoma", (int) (Math.random() * 30), SWT.NORMAL));
shell.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
drawshit(e.gc);
}
});
// drawshit(shell);
while (!shell.isDisposed()) {
if (display.readAndDispatch()) {
}
// drawshit(shell);
else {
display.sleep();
}
}
font.dispose();
display.dispose();
// AWT
AWTTest test = new AWTTest();
System.in.read();
}
// private static void drawshit(Shell shell) {
private static void drawshit(GC gc) {
// if (!shell.isDisposed()) {
// GC gc = new GC(shell);
// Rectangle bounds = shell.getBounds();
long start = System.nanoTime();
for (int i = 0; i < 1000; i++) {
// try various different things here, e.g. drawing lines, rectangles, cacheing fonts etc.
gc.setFont(font);
gc.drawText("Hello World", (int) (Math.random() * 400), (int) (Math.random() * 400));
}
long stop = System.nanoTime();
// gc.dispose();
System.out.println("SWT time: " + (stop - start) / 1000000 + " ms");
// }
}
}
-
Attachment: SWTTest.java
(Size: 1.73KB, Downloaded 180 times)
|
|
|
Re: SWT performance on drawing text (compare with Swing, sample code provided) [message #847548 is a reply to message #847216] |
Tue, 17 April 2012 11:29 |
Leung Wang Hei Messages: 64 Registered: July 2010 |
Member |
|
|
Like you said, I moved font creation/disposal out of the drawing loop.
I am able to lower the SWT time from 80ms to 60ms (25%-)
but still AWT gives me less than 30ms.
What am I missing here?
Ludwig, should your middle "AWT time: 137 ms" be "SWT time: 137 ms" ?
On 4/17/2012 1:18 PM, Ludwig Moser wrote:
> i changed the SWT code to proper useage of the fonts (create fonts
> before doing the test and disposing it afterwards.
> results here are:
>
> SWT time: 74 ms
> AWT time: 137 ms
> AWT time: 75 ms
>
> note that i have a crappy old "Pentium D" cpu ;)
package SWT.vs.J2D;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SWTTest {
private static Font font;
public static void main(String[] args) throws IOException {
Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(0, 0, 500, 500);
shell.open();
font = new Font(display, new FontData("Tahoma", (int) (Math.random() * 30), SWT.NORMAL));
shell.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
drawshit(e.gc);
}
});
// drawshit(shell);
while (!shell.isDisposed()) {
if (display.readAndDispatch()) {
}
// drawshit(shell);
else {
display.sleep();
}
}
font.dispose();
display.dispose();
// AWT
AWTTest test = new AWTTest();
System.in.read();
}
// private static void drawshit(Shell shell) {
private static void drawshit(GC gc) {
// if (!shell.isDisposed()) {
// GC gc = new GC(shell);
// Rectangle bounds = shell.getBounds();
long start = System.nanoTime();
for (int i = 0; i < 1000; i++) {
// try various different things here, e.g. drawing lines, rectangles, cacheing fonts etc.
gc.setFont(font);
gc.drawText("Hello World", (int) (Math.random() * 400), (int) (Math.random() * 400));
}
long stop = System.nanoTime();
// gc.dispose();
System.out.println("SWT time: " + (stop - start) / 1000000 + " ms");
// }
}
}
-
Attachment: SWTTest.java
(Size: 1.73KB, Downloaded 179 times)
|
|
|
Re: SWT performance on drawing text (compare with Swing, sample code provided) [message #847553 is a reply to message #847216] |
Tue, 17 April 2012 11:29 |
Leung Wang Hei Messages: 64 Registered: July 2010 |
Member |
|
|
Like you said, I moved font creation/disposal out of the drawing loop.
I am able to lower the SWT time from 80ms to 60ms (25%-)
but still AWT gives me less than 30ms.
What am I missing here?
Ludwig, should your middle "AWT time: 137 ms" be "SWT time: 137 ms" ?
On 4/17/2012 1:18 PM, Ludwig Moser wrote:
> i changed the SWT code to proper useage of the fonts (create fonts
> before doing the test and disposing it afterwards.
> results here are:
>
> SWT time: 74 ms
> AWT time: 137 ms
> AWT time: 75 ms
>
> note that i have a crappy old "Pentium D" cpu ;)
package SWT.vs.J2D;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SWTTest {
private static Font font;
public static void main(String[] args) throws IOException {
Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(0, 0, 500, 500);
shell.open();
font = new Font(display, new FontData("Tahoma", (int) (Math.random() * 30), SWT.NORMAL));
shell.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
drawshit(e.gc);
}
});
// drawshit(shell);
while (!shell.isDisposed()) {
if (display.readAndDispatch()) {
}
// drawshit(shell);
else {
display.sleep();
}
}
font.dispose();
display.dispose();
// AWT
AWTTest test = new AWTTest();
System.in.read();
}
// private static void drawshit(Shell shell) {
private static void drawshit(GC gc) {
// if (!shell.isDisposed()) {
// GC gc = new GC(shell);
// Rectangle bounds = shell.getBounds();
long start = System.nanoTime();
for (int i = 0; i < 1000; i++) {
// try various different things here, e.g. drawing lines, rectangles, cacheing fonts etc.
gc.setFont(font);
gc.drawText("Hello World", (int) (Math.random() * 400), (int) (Math.random() * 400));
}
long stop = System.nanoTime();
// gc.dispose();
System.out.println("SWT time: " + (stop - start) / 1000000 + " ms");
// }
}
}
-
Attachment: SWTTest.java
(Size: 1.73KB, Downloaded 189 times)
|
|
|
Re: SWT performance on drawing text (compare with Swing, sample code provided) [message #847560 is a reply to message #847216] |
Tue, 17 April 2012 11:29 |
Leung Wang Hei Messages: 64 Registered: July 2010 |
Member |
|
|
Like you said, I moved font creation/disposal out of the drawing loop.
I am able to lower the SWT time from 80ms to 60ms (25%-)
but still AWT gives me less than 30ms.
What am I missing here?
Ludwig, should your middle "AWT time: 137 ms" be "SWT time: 137 ms" ?
On 4/17/2012 1:18 PM, Ludwig Moser wrote:
> i changed the SWT code to proper useage of the fonts (create fonts
> before doing the test and disposing it afterwards.
> results here are:
>
> SWT time: 74 ms
> AWT time: 137 ms
> AWT time: 75 ms
>
> note that i have a crappy old "Pentium D" cpu ;)
package SWT.vs.J2D;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SWTTest {
private static Font font;
public static void main(String[] args) throws IOException {
Display display = new Display();
Shell shell = new Shell(display);
shell.setBounds(0, 0, 500, 500);
shell.open();
font = new Font(display, new FontData("Tahoma", (int) (Math.random() * 30), SWT.NORMAL));
shell.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
drawshit(e.gc);
}
});
// drawshit(shell);
while (!shell.isDisposed()) {
if (display.readAndDispatch()) {
}
// drawshit(shell);
else {
display.sleep();
}
}
font.dispose();
display.dispose();
// AWT
AWTTest test = new AWTTest();
System.in.read();
}
// private static void drawshit(Shell shell) {
private static void drawshit(GC gc) {
// if (!shell.isDisposed()) {
// GC gc = new GC(shell);
// Rectangle bounds = shell.getBounds();
long start = System.nanoTime();
for (int i = 0; i < 1000; i++) {
// try various different things here, e.g. drawing lines, rectangles, cacheing fonts etc.
gc.setFont(font);
gc.drawText("Hello World", (int) (Math.random() * 400), (int) (Math.random() * 400));
}
long stop = System.nanoTime();
// gc.dispose();
System.out.println("SWT time: " + (stop - start) / 1000000 + " ms");
// }
}
}
-
Attachment: SWTTest.java
(Size: 1.73KB, Downloaded 229 times)
|
|
|
Re: SWT performance on drawing text (compare with Swing, sample code provided) [message #847561 is a reply to message #846740] |
Tue, 17 April 2012 11:34 |
Leung Wang Hei Messages: 64 Registered: July 2010 |
Member |
|
|
I think font is disposed right after drawing the text.
You are right, normally font and colour should be cached. However, for
this particular text drawing test, the time difference is really
disappointing.
even with Ludwig's change, the SWT test is still 2 times slower than
AWT, at least apparently.
I got a sense that something is missing. Else there is no point for
anybody using SWT. What's missing?
On 4/16/2012 10:33 PM, Ed Merks wrote:
> Probably most of the cost is creating the fonts. You'd never create
> fonts and then not dispose them in this way for SWT. You're run out of
> handles with that approach.
>
>
> On 16/04/2012 12:03 PM, Leung Wang Hei wrote:
>> All,
>>
>> A classic question but it really surprise me for the difference in
>> numbers.
>>
>> Attached simple tests in Swing and SWT, drawing text with random fonts
>> 1000 times.
>>
>> SWT (gc.advance off)
>> ~80ms for every 1000 text drawn
>>
>> SWT (gc.advance on)
>> ~200ms for every 1000 text drawn
>>
>> Swing (with Anti-aliasing hints on)
>> ~30ms for every 1000 text drawn
>>
>>
>> Config:
>> Win7
>> Java 1.6_25
>> SWT org.eclipse.swt.win32.win32.x86_3.7.1.v3738a.jar
>>
>>
>>
>> Thanks for you all.
|
|
| |
Re: SWT performance on drawing text (compare with Swing, sample code provided) [message #848356 is a reply to message #848196] |
Wed, 18 April 2012 06:33 |
Ludwig Moser Messages: 476 Registered: July 2009 |
Senior Member |
|
|
i cant reproduce your 'swt is slower'
SWT time: 74 ms
AWT time: 137 ms
AWT time: 75 ms
says swt is about the same speed as AWT... if its done right with the font management.
you cant compare your testing results with mine as your computer seems (way) faster than mine
what i did was creating a cache for 1000 fonts, generate the fonts. (app init)
then render the swt
then dispose the fonts again (when app terminates)
Quote:Ludwig, should your middle "AWT time: 137 ms" be "SWT time: 137 ms" ?
nope, there seems one swt test and two awt tests from your example (at least thats what i got on output)
so average AWT time was 106ms
swt time was 74ms
-- for 1000 drawings
NOTE: i only edited the SWT code...
[Updated on: Wed, 18 April 2012 06:34] Report message to a moderator
|
|
|
Re: SWT performance on drawing text (compare with Swing, sample code provided) [message #848397 is a reply to message #848196] |
Wed, 18 April 2012 07:24 |
Thomas Schindl Messages: 6651 Registered: July 2009 |
Senior Member |
|
|
SWT uses native widgets, whereas swing/awt draws everything on their
own, the thing here is that even if SWT-Drawing is not as fast as
AWT-drawing this doesn't really hurt SWT-UIs because most of the UI is
anways made up of native controls.
Tom
Am 18.04.12 04:31, schrieb Leung Wang Hei:
> Some suggested ClearType in Win7 added extra penalty. Sadly, turning
> that off gives no obvious improvement (still ~60ms for SWT)
>
> I understand SWT is based on a very different design with AWT and my
> measuring is properly not valid on non-Window OS.
>
> Even so, does it imply there is some unavoidable slowness when using SWT
> on Win?
>
> In another word, what would be a "more fair" testing code for SWT and AWT?
>
>
> Greatly appreciated for any advice. This problem really frustrated me.
>
>
> On 4/17/2012 7:29 PM, Leung Wang Hei wrote:
>> Like you said, I moved font creation/disposal out of the drawing loop. I
>> am able to lower the SWT time from 80ms to 60ms (25%-)
>>
>> but still AWT gives me less than 30ms.
>>
>> What am I missing here?
>>
>>
>>
>>
>> Ludwig, should your middle "AWT time: 137 ms" be "SWT time: 137 ms" ?
>>
>>
>>
>> On 4/17/2012 1:18 PM, Ludwig Moser wrote:
>>> i changed the SWT code to proper useage of the fonts (create fonts
>>> before doing the test and disposing it afterwards.
>>> results here are:
>>>
>>> SWT time: 74 ms
>>> AWT time: 137 ms
>>> AWT time: 75 ms
>>>
>>> note that i have a crappy old "Pentium D" cpu ;)
>>
>
|
|
|
Re: SWT performance on drawing text (compare with Swing, sample code provided) [message #849556 is a reply to message #848397] |
Thu, 19 April 2012 08:51 |
Leung Wang Hei Messages: 64 Registered: July 2010 |
Member |
|
|
You are right Tom. The problem is we are heavily using canvas (KTable
in precise), which goes back to the plain drawing function of SWT.
On 4/18/2012 3:24 PM, Tom Schindl wrote:
> SWT uses native widgets, whereas swing/awt draws everything on their
> own, the thing here is that even if SWT-Drawing is not as fast as
> AWT-drawing this doesn't really hurt SWT-UIs because most of the UI is
> anways made up of native controls.
>
> Tom
>
> Am 18.04.12 04:31, schrieb Leung Wang Hei:
>> Some suggested ClearType in Win7 added extra penalty. Sadly, turning
>> that off gives no obvious improvement (still ~60ms for SWT)
>>
>> I understand SWT is based on a very different design with AWT and my
>> measuring is properly not valid on non-Window OS.
>>
>> Even so, does it imply there is some unavoidable slowness when using SWT
>> on Win?
>>
>> In another word, what would be a "more fair" testing code for SWT and AWT?
>>
>>
>> Greatly appreciated for any advice. This problem really frustrated me.
>>
>>
>> On 4/17/2012 7:29 PM, Leung Wang Hei wrote:
>>> Like you said, I moved font creation/disposal out of the drawing loop. I
>>> am able to lower the SWT time from 80ms to 60ms (25%-)
>>>
>>> but still AWT gives me less than 30ms.
>>>
>>> What am I missing here?
>>>
>>>
>>>
>>>
>>> Ludwig, should your middle "AWT time: 137 ms" be "SWT time: 137 ms" ?
>>>
>>>
>>>
>>> On 4/17/2012 1:18 PM, Ludwig Moser wrote:
>>>> i changed the SWT code to proper useage of the fonts (create fonts
>>>> before doing the test and disposing it afterwards.
>>>> results here are:
>>>>
>>>> SWT time: 74 ms
>>>> AWT time: 137 ms
>>>> AWT time: 75 ms
>>>>
>>>> note that i have a crappy old "Pentium D" cpu ;)
>>>
>>
>
|
|
| |
Goto Forum:
Current Time: Fri Apr 26 21:30:12 GMT 2024
Powered by FUDForum. Page generated in 0.03594 seconds
|