SWT performance on drawing text (compare with Swing, sample code provided) [message #846732] |
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 258 times) -
Attachment: AWTTest.java
(Size: 2.40KB, Downloaded 217 times)
|
|
|
Powered by
FUDForum. Page generated in 0.03456 seconds