Dienstag, 5. März 2013

Doing the same but beeing inefficient

Sometimes it seems that this is the maxim of a typical Java developer. Take a look at two ways to build a Java program.

This is the old fashioned legacy way a developer would build something, he will write a Makefile. And it might look like this:

all jar: xmuta.jar

clean:
        -rm *.class xmuta.jar *~

compile: xmuta.class

xmuta.jar: manifest xmuta.class
        jar cvfm $@ $< *.class

xmuta.class: xmuta.java
        javac $<

The Makefile contains the rules for compilation and packaging. It takes 167 characters to write everything down.

Now take a look at the new style by writing an XML file (oh God how modern). Okay somebody might argue, that Ant is already old fashioned but there is one thing sure in this world: with Maven it gets even worse. So I consider only Ant:

<project>
  <target name="clean">
    <delete>
      <fileset dir="." includes="*.class"/>
      <fileset dir="." file="xmuta.class"/>
      <fileset dir="." includes="*~"/>
    </delete>
  </target>
  <target name="compile">
    <javac srcdir="." destdir="." debug="true"/>
  </target>
  <target name="jar" depends="compile">
    <jar destfile="xmuta.jar" basedir="." 
         manifest="manifest" includes="*.class"/>
  </target>
</project>

And what does "wc -c"say? 443 characters. I do not know whether taking 443 characters is much more secure but it is definitely more than twice of the code the old fashioned guy has to write down. So people who like to hear themselves should write build files instead of make files.

But how about the time you have to spend for compilation? Be warned: if you have a sensitive stomach, stop reading here.

This is the time GNU make takes.

$ time make jar
javac xmuta.java
jar cvfm xmuta.jar manifest *.class
added manifest
adding: xmuta$1.class(in = 866) (out= 514)(deflated 40%)
adding: xmuta.class(in = 2075) (out= 1100)(deflated 46%)

real    0m0.608s
user    0m0.547s
sys     0m0.099s

And this is the time Apache Ant takes.

$ time ant jar
Buildfile: build.xml

compile:
    [javac] Compiling 1 source file to /home/users/ceving/xmuta

jar:
      [jar] Building jar: /home/users/ceving/xmuta/xmuta.jar

BUILD SUCCESSFUL
Total time: 1 second

real    0m1.544s
user    0m2.099s
sys     0m0.128s

Great isn't it? Only 253% instead of 265%. Can you feel the power of youth?

Now you can understand why it is necessary for a Ant or Maven developer see with every build run the time it takes to build the program. They have to become dulled to endure the misery.