Wednesday, April 9, 2008

Is Swing slow?

The main problem with this question is the question itself, is a Windows application slow? is the minesweeper slow? are the legacy applications slow?



Everything depends on how you use it, if you have a car, lets say Ferrari, if you use it in the first gear all the time the answer will be "YES THE FERRARI IS THE SLOWEST CAR IN THE WORLD", but is it true? is the Ferrari slow? or is the driver the main problem? you would agree that the driver will have to take some lessons... ok... now let me do the question again: "Is Swing slow?" the answer: NO, Swing is as slow as the architect and developers are, if the swing applications have problems then we will not change the Framework, we need to change the Driver!



The architect and the developer must consider how the platform works under some circumstances and take care of the details, that's true even if you work on a windows application. Swing provides some advantages that you could not have in other languages, but it comes with responsibilities.



But, the theory could say something different from the experience, so lets put hands on and measure some times from the sample posted in the last article Swing Fast sample.



.NET vs Swing Time measure

I created a very simple class to measure the loading time in both applications, the following is the class I used in Java:




public class Timer {
private static long startTime;

public static void registerStartTime() {
startTime = Calendar.getInstance().getTimeInMillis();
}

public static long getElapsedTime() {
long currentTime = Calendar.getInstance().getTimeInMillis();

return currentTime - startTime;
}
}


and here's the .NET version:




public class Timer
{
private static long startTime;

public Timer()
{
}

public static void registerStartTime()
{
startTime = DateTime.Now.Ticks;
}

public static long getElapsedTime()
{
long currentTime = DateTime.Now.Ticks;
return currentTime - startTime;
}
}


ok, I put the statement Timer.registerStartTime(); in the main code just before the invocation of the screen, and the Message.show("Time: " + Timer.getElapsedTime()); just after the screen loaded. These are the results:




















Java ms..NET ms.
187156
125156
125156
125156
125156



(sorry for the extra space, I don't know why the blogger added this :s)

The average time for Java is: 137.5ms and 156ms for .NET, off course... I will not say at this moment that java is faster than .net, this is just the simplest sample, we need to do more tests and more complex scenarios. But the point is... if the starting time is pretty close, why the application will be slower in java than in .net? again, everything depends on how you use the technology.

New Scenario

Ok, what if we increase the number of components and the data on them? the new scenario has 4 new combos, each one with the letters from "a" to "z" as items, so what's now?





















Java ms..NET ms.
156156
157156
156156
156156
156156
157156


So, now the times are very close, right? ok, still too easy right?, I added a JTable in the swing application with 3 Columns and 20 Rows and the time is still the same 156ms!!!.

Conclusion

Most of the problems with the Client applications are not related with the platform or the framework, most of them are because of the abuse of the code, some developers (and also the architects) believe that the code in the client could do whatever they want and do a lot of rookie mistakes (I will talk about that in a new article). The main idea of this article is to prove you that Swing is not slower than other languages, most of the problems with the Swing applications are related with some of the following problems:

- Misunderstanding of the caching systems in the jndi lookups.
- Abuse of the workload in the Constructor of the class
- Abuse of the round trips to the server (when the screen gets the data for combos, tables, etc.)
- Coding problems because the Notepad coding technique.
- Performance problems in the JDK 1.4

and some others I will work on later, with their solutions.

We're close to the main topic of this series, how to build an enterprise applications with Swing as front-end and EJB as backend.

1 comment:

Anonymous said...

I agree with everything in your post. Rich client performance issues are a thing of the past. However crap coders are still with us. A crap coder making a crap Swing application does not mean swing is crap