users@jaxb.java.net

Is unmarshalling from a file the same speed as a ByteArrayInputStream?

From: Forsberg, Mike <mike.forsberg_at_amd.com>
Date: Fri, 9 Jan 2009 12:48:29 -0600

I've seemed to have run across some interesting behavior in some code
that I've inherited...

 

Using the same unmashaller, the unmarshalling from a file appears to run
in the same time as unmarshalling of a string. I'm wondering if someone
could shed some information on why this is happening.

 

Consider the following code...

 

Temp file based unmarshalling (What I would consider bad mojo)

 

// exceptions are simplified for this example.

Public Model unmarshallModelViaFile(String xmlString) throws Exception {

    File f = File.createTempFile("tempFile", ".xml", tmpDir);

    FileWriter fw = new FileWriter(f);

    fw.write(xmlString);

    fw.close();

    Model model = (Model) modelUnmarshaller.unmarshal(f);

    return model;

}

 

String based unmarshalling... (What I think we should do)

 

// exceptions are simplified for this example.

Public Model unmarshallModel(String xmlString) throws Exception {

    Model model = (Model) modelUnmarshaller.unmarshal(new
ByteArrayInputStream(xmlString.getBytes()));

    return model;

}

 

When I run these two functions in a loop of 1000 times, they only vary
in time by 1%.

 

My timing code...

 

long fileStart = System.currentTimeMillis();

for (int i = 0; i < 1000; i++) {

    Model model2 = marshaller.unmarshallModelViaFile(contentsOfFile);

}

long fileDuration = System.currentTimeMillis() - fileStart;

System.out.println("fileDuration : " + fileDuration);

 

Same timing code for the String based version...

 

Could someone inform me why the elapsed time of the two methods is so
similar?

 

Mike Forsberg
Factory Automation Engineer II
TDG - MST - MS - ACS
T 512.602.0220

 

 






image001.gif
(image/gif attachment: image001.gif)