Skip to main content

Swing vs. AWT: Performance

9 replies [Last post]
afishionado
Offline
Joined: 2004-05-26
Points: 0

After listening to Sun talk about how much faster Swing is than AWT, and listening to a C programmer tell me that there's no reason "lightweight" components should be faster than native components, I decided to find out for myself, and wrote a quick benchmark (code below).

The benchmark creates and destroys 100 windows, each containing 100 buttons. Initially the benchmark tested AWT and Swing in one run, but I realized that AWT is loaded when Swing is loaded, and feared that might mess up the time; now you can test AWT or Swing via a command line switch.

It seems that Sun was right--Swing is nearly twice as fast as AWT, on my machine at least (2 GHz Athlon, GeForce 4 video car, running SuSE 9.0, KDE, and Sun's J2SE 1.4.2). I don't happen to have SWT installed right now, but I would be interested to see how it comes out--my guess is that it would probably beat both AWT and Swing. It might be also interesting to test components besides buttons. :-)

William

<br />
import java.awt.*;<br />
import javax.swing.*;</p>
<p>public class AWTSwingBench {<br />
    public static void main(String args[]) throws Exception {<br />
        if (args.length < 1) {<br />
            System.out.println("Usage: AWTSwingBench [AWT|Swing]");<br />
            System.exit(1);<br />
        }</p>
<p>        if (args[0].equals("AWT")) {<br />
            testAWT();<br />
        } else if (args[0].equals("Swing")) {<br />
            testSwing();<br />
        } else {<br />
            System.out.println("Usage: AWTSwingBench [AWT|Swing]");<br />
        }<br />
    }</p>
<p>    public static void testAWT() {<br />
        long awtTime = 0;<br />
        for (int counter = 0; counter < 100; ++counter) {<br />
            awtTime += awtBench();<br />
        }</p>
<p>        System.out.println("AWT:   " + awtTime + " milliseconds");<br />
    }</p>
<p>    public static void testSwing() {<br />
        long swingTime = 0;</p>
<p>        for (int counter = 0; counter < 100; ++counter) {<br />
            swingTime += swingBench();<br />
        }</p>
<p>        System.out.println("Swing: " + swingTime + " milliseconds");<br />
    }</p>
<p>    public static long awtBench() {<br />
        long time = 0;<br />
        long start = System.currentTimeMillis();<br />
        Frame frame = new Frame("AWT benchmark");<br />
        frame.setLayout(new GridLayout(10, 10));<br />
        for (int counter = 0; counter < 100; ++counter)<br />
            frame.add(new Button("Button"));<br />
        frame.pack();<br />
        frame.show();<br />
        frame.dispose();<br />
        time = System.currentTimeMillis() - start;</p>
<p>        return time;<br />
    }</p>
<p>    public static long swingBench() {<br />
        long time = 0;<br />
        long start = System.currentTimeMillis();<br />
        JFrame jframe = new JFrame("Swing benchmark");<br />
        Container c = jframe.getContentPane();<br />
        c.setLayout(new GridLayout(10, 10));<br />
        for (int counter = 0; counter < 100; ++counter)<br />
            c.add(new JButton("JButton"));<br />
        jframe.pack();<br />
        jframe.show();<br />
        jframe.dispose();<br />
        time = System.currentTimeMillis() - start;</p>
<p>        return time;<br />
    }<br />
}<br />

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
yuriymikhaylovskiy
Offline
Joined: 2010-12-19
Points: 0

http://opt.sourceforge.net/ Java Micro Benchmark - control tasks required to determine the comparative performance characteristics of the computer system on different platforms. Can be used to guide optimization decisions and to compare different Java implementations.

shemnon
Offline
Joined: 2003-06-11
Points: 0

I don't see that the non-blocking state of show() makes a difference. I added Robot.waitForIdle in both and also an invokeAndWait.paintImmediately in the swingBench and I still get swing as about 4x faster....

These times are from what Is posted below...

C:\Temp>java AWTSwingBench AWT
AWT: 24545 milliseconds

C:\Temp>java AWTSwingBench Swing
Swing: 6469 milliseconds

[code]
public static long awtBench(Robot robot) {
long time = 0;
long start = System.currentTimeMillis();
Frame frame = new Frame("AWT benchmark");
frame.setLayout(new GridLayout(10, 10));
for (int counter = 0; counter < 100; ++counter)
frame.add(new Button("Button"));
frame.pack();
frame.show();
/*robot.waitForIdle();*/
frame.dispose();
time = System.currentTimeMillis() - start;

return time;
}

public static long swingBench(Robot robot) {
long time = 0;
long start = System.currentTimeMillis();
final JFrame jframe = new JFrame("Swing benchmark");
Container c = jframe.getContentPane();
c.setLayout(new GridLayout(10, 10));
for (int counter = 0; counter < 100; ++counter)
c.add(new JButton("JButton"));
jframe.pack();
jframe.show();
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
JRootPane rp = jframe.getRootPane();
rp.paintImmediately(rp.getBounds());
}
});
} catch (Exception ie) {
ie.printStackTrace();
}
/*robot.waitForIdle();*/
jframe.dispose();
time = System.currentTimeMillis() - start;

return time;
}

// these re-use robot from the test methods.
[/code]

trembovetski
Offline
Joined: 2003-12-31
Points: 0

That's mostly because pack() makes the components displayable, and most of the time creating the peers and such is spent there.

I think it you remove pack(), you'll see the difference.

Thanks,
Dmitri

rufwork
Offline
Joined: 2004-02-17
Points: 0

Ah, more Windows-centricity (which I'd agree is the best place to start).

At any rate, here's some results from Mac OS X 10.3, with whatever resources running that I usually have running when I'm hacking around -- which is to say this isn't exactly a clean room. But the results are still interesting:

iBookG4:~ smack$ java AWTSwingBench AWT
AWT: 30548 milliseconds
iBookG4:~ smack$ java AWTSwingBench AWT
AWT: 30509 milliseconds
iBookG4:~ smack$ java AWTSwingBench Swing
Swing: 112100 milliseconds
iBookG4:~ smack$ java AWTSwingBench Swing
Swing: 112828 milliseconds

Swing is approaching four times slower than AWT in Jobsland.

All about the implementation. Looks like this is another example of Swing apologia built into the runtime, which reminds me of when Netbeans went well out of their way to ensure it acted more Windows-like. That is to say, non-trivial resources seem to have been expended to give Swing the upper hand, as the expected result does, indeed, happen in at least one alternative implementation of Java.

Who has a blackdown.org example? ;^)

simoncolston
Offline
Joined: 2004-10-21
Points: 0

> Ah, more Windows-centricity

Really? I thought Suse was Linux?

afishionado
Offline
Joined: 2004-05-26
Points: 0

Yeah ... I'm confused too. I see forward slashes in the filenames in Konsole, so it must be Linux. :-)

rufwork
Offline
Joined: 2004-02-17
Points: 0

Ack! [slinks away] That does make this more interesting. s/blackdown.org/WindowsXP/gi

Still, I guess I'll fall back on this being an official Sun release (daggum x86 bias!), and I suppose I'll guess that Windows would turn up something similiar. It would be interesting to chart at what point Swing caught AWT; I'm betting, say, 1.2 turns up results a bit closer to what I saw on the Mac, which allows me to continue thinking there's a conspiracy to push Swing by throwing resources that might have been better spent integrating SWT or expanding AWT.

trembovetski
Offline
Joined: 2003-12-31
Points: 0

Well, as flattering as this may be to Swing, I don't think this is a valid benchmark. For example, you're mostly tesing only one aspect - creation/addiiton of the components.

Note that since the show() method is non-blocking, you're not really testing much with it.

Gui testing (and benchmarking) is tricky: you need to decide what exaclty are you measuring: is it repainting performance, responsiveness, time it takes to construct gui, dispose of it. The responsiveness factor is very important, but not easy to define and benchmark.

With 1.5 it gets even trickier to compare the two toolkits, since with the addition of XAWT toolkit (default toolkit on solaris/linux in mustang) AWT uses Java2D and Swing for rendering..

Thanks,
Dmitri
Java2D Team

afishionado
Offline
Joined: 2004-05-26
Points: 0

Yes, I know, this was just thrown together when I was feeling bored. :-)

I didn't realize that show() was non-blocking. There's no mention of this in the Javadocs (as far as I can tell), and I've never delved into the AWT code myself.

So, yes, it's a fairly naive benchmark. All I was after was to see if there was anything very obviously "wrong" going on.