Sunita<br />SCJP 1.4
-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
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.
Sunita<br />SCJP 1.4
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
]