Aren't all of the underlying implementations boiling down to the same one,
which ends up using an InputStream from all of the wrappers/helpers that
allow for other types (String, File) etc?
I would think this is the case. and if it were, the timings should infact be
pretty close to identical
Alex Sherwin
alex.sherwin_at_acadiasoft.com
From: Forsberg, Mike [mailto:mike.forsberg_at_amd.com]
Sent: Friday, January 09, 2009 1:48 PM
To: users_at_jaxb.dev.java.net
Subject: Is unmarshalling from a file the same speed as a
ByteArrayInputStream?
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