Meaningless Drivel is fun!
The moose likes Swing / AWT / SWT and the fly likes What's faster awt or swing ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "What Watch "What New topic

What's faster awt or swing ?

Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
I thought it was awt but my friend says swing. Who's right ?
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
I read that Swing is faster becos it uses MVC architecture...

Sunita<br />SCJP 1.4
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

It depends on what you mean by "faster" and in what regards.
AWT uses Native Library calls to draw all of it's components. SWING uses the Graphics2D API to draw all of it's components. So, naturally, AWT would be a bit faster in regards to drawing itself. As far as everything else I would say they are about the same.
SUN is working hard to improve the Graphics2D performance for the 1.5 release of the JDK/JRE which will also improve the performance of SWING. Again, I say all this in regards to rendering the components.

GenRocket - Experts at Building Test Data
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

"Fast" depends on multiple factors... type of machine it is running on (133MHz vs. 2GHz), complexity of the program, etc. Plus the majority of speed of a program has to do with how much I/O it does (to disk, database, or network) than it's GUI.

If it comes down to simple speed that the GUI can draw itself (which I have no idea how you would actually measure), I would guess AWT because it is using native widgets to actually render the GUI. Swing only has a native window and draws all the components on top of it. (Maybe your friend was thinking that the HotSpot JVM would be able to dynamically optimize the Java Swing components to be able to render faster than native components? It could be theoretically possible... but I really doubt it... )

Besides, there's only so fast that the human eye can see, and the computer monitor can refresh...

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
I am new to Swing, so I guess I misinterpreted.. This was what I read from jfc unleashed chapter 2

When evaluating a toolkit, performance measures such as "How long does it take to add ten thousand items to a list?" are often considered. These types of measurements for properly written JFC applications are irrelevant. The reason for this is that ten thousand items are never added to a list. Instead, the data source implements the ListModel interface and is immediately available for display. The difference in these two architectures is demonstrated in the following example:
package com.foley.test;
import java.awt.*;
import javax.swing.*;
* A simple example to time adding items to an AWT List and
* JFC JList components.
* @author Mike Foley
public class TestList {

* Application entry point.
* The arguments are not used.
* @param args Command line arguments passed to the application.
public static void main( String[] args ) {
// Time adding Strings to an AWT List component.
long d = System.currentTimeMillis();
List awtList = new List();
for( int i = 0; i < 10000; i++ ) {
awtList.add( "Item " + i );
System.out.println( "AWT time: " +
( System.currentTimeMillis() - d ) );

// Time creating a ListModel and adding it to
// a JList component.
d = System.currentTimeMillis();
ListModel model = new AbstractListModel() {
public int getSize() { return( 10000 ); }
public Object getElementAt( int index )
{ return( "Item " + index ); }
} ;
JList jfcList = new JList( model );
System.out.println( "JFC time: " +
(System.currentTimeMillis() - d ) );
When running this example on a 300MHz Pentium II processor computer with 128MB of RAM, the AWT List creation averaged a whopping 1.65 seconds while the JFC JList creation averaged approximately 0.015 seconds. The JList was two orders of magnitude faster. This obviously is a jaded example because the objects for the JList are lazily created when requested by the view. However, it approximates many real-world programming examples when the data to be displayed is already in a data structure in memory or can be lazily evaluated when, and if, requested. The JFC architecture gives the application developer the flexibility to store and/or evaluate data in the best way for your particular application. It does not force an assumed structure on the data.

[ April 03, 2003: Message edited by: sun par ]
Andrew Trumper

Joined: Sep 08, 2001
Posts: 12
Originally posted by sun par:
I am new to Swing, so I guess I misinterpreted.. This was what I read from jfc unleashed chapter 2

Speed really does mean different things to different people. In the example your citing hey are not comparing the same thing. In one case someone is adding items to a list one at a time. After each item is added the list is ready to be used, the scrollbars are update etc.. in the other case not only are all the items being added at once, but the "items" (The strings) are being generated on the fly and are therefore not stored anywhere.
When I read of swing vs awt "speed" comparisons I always think of GUI widget responsiveness. Swing GUIs almost always feel more sluggish than AWT GUIs. The reasone for this is the widgets in an awt program do most of their painting and whatnot in native code with native system widgets. Awt object usualy only go into java code for events and some resizing code. Swing objects by comparison are always emulating a look and feel and do so in java code using an abstracted graphics library. There is quite a bit of overhead associated with that.
I have written my own List object using an awt scrollpane and a pane. Adding 10 000 items to it takes very little time (about 100ms) as in addition to an add(Object o) function I have an add(Object[] o) function for batch adding. The most expensive opperation is either sorting all the list items or copying them across.. I don't allow someone to create a list from a data model or play with the internal data model as that would bring up some nasty threading issues which swing ignores but I really can't.
I agree. Here's the link:
subject: What's faster awt or swing ?
It's not a secret anymore!