<br><font size=2 face="sans-serif">I tried using the latest (non queue version) and got the following, shows some quite dramatic savings on large files.</font>
<br>
<br><font size=2 color=blue face="Courier New">Reuse=true size=1884 time: 595</font>
<br><font size=2 color=blue face="Courier New">Reuse=false size=1884 time: 187</font>
<br>
<br><font size=2 color=blue face="Courier New">Reuse=true size=21363 time: 440</font>
<br><font size=2 color=blue face="Courier New">Reuse=false size=21363 time: 438</font>
<br>
<br><font size=2 color=blue face="Courier New">Reuse=true size=42527 time: 703</font>
<br><font size=2 color=blue face="Courier New">Reuse=false size=42527 time: 563</font>
<br>
<br><font size=2 color=blue face="Courier New">Reuse=true size=318591 time: 8420</font>
<br><font size=2 color=blue face="Courier New">Reuse=false size=318591 time: 5503</font>
<br>
<br><font size=2 color=blue face="Courier New">Reuse=true size=743381 time: 23266</font>
<br><font size=2 color=blue face="Courier New">Reuse=false size=743381 time: 13844</font>
<br>
<br><font size=2 face="sans-serif">However, I then changed the code to use _builder in both the if and else (just to see what the natural variation would be like) and got the following, now I am really confused as the re-use = false does the same stuff as re-use = true, it also instantiates a new object and it still takes less time.</font>
<br>
<br><font size=2 color=#820040 face="Courier New"><b>if</b></font><font size=2 face="Courier New"> (_reuse) {</font>
<br><font size=2 face="Courier New"> _builder.build(source);</font>
<br><font size=2 face="Courier New">} </font><font size=2 color=#820040 face="Courier New"><b>else</b></font><font size=2 face="Courier New"> {</font>
<br><font size=2 face="Courier New"> SAXBuilder builder = </font><font size=2 color=#820040 face="Courier New"><b>new</b></font><font size=2 face="Courier New"> SAXBuilder();</font>
<br><font size=2 face="Courier New"> _builder.build(source);</font>
<br><font size=2 face="Courier New">}</font>
<br>
<br><font size=2 color=blue face="Courier New">Reuse=true size=1884 time: 513</font>
<br><font size=2 color=blue face="Courier New">Reuse=false size=1884 time: 140</font>
<br>
<br><font size=2 color=blue face="Courier New">Reuse=true size=21363 time: 406</font>
<br><font size=2 color=blue face="Courier New">Reuse=false size=21363 time: 297</font>
<br>
<br><font size=2 color=blue face="Courier New">Reuse=true size=42527 time: 594</font>
<br><font size=2 color=blue face="Courier New">Reuse=false size=42527 time: 734</font>
<br>
<br><font size=2 color=blue face="Courier New">Reuse=true size=318591 time: 7719</font>
<br><font size=2 color=blue face="Courier New">Reuse=false size=318591 time: 5486</font>
<br>
<br><font size=2 color=blue face="Courier New">Reuse=true size=743381 time: 22297</font>
<br><font size=2 color=blue face="Courier New">Reuse=false size=743381 time: 14423</font><font size=2 face="sans-serif"><br>
<br>
/Phill<br>
IS Dept, Software Engineer.<br>
phill_perryman@mitel.com<br>
http://www.mitel.com<br>
Tel: +44 1291 436023</font>
<br>
<br>
<br>
<table width=100%>
<tr valign=top>
<td>
<td><font size=1 face="sans-serif"><b>Per Norrman <per.norrman@austers.se></b></font>
<br><font size=1 face="sans-serif">Sent by: jdom-interest-bounces@servlets.com</font>
<p><font size=1 face="sans-serif">31/08/2004 07:39</font>
<br>
<td><font size=1 face="Arial"> </font>
<br><font size=1 face="sans-serif"> To: David Wall <d.wall@computer.org></font>
<br><font size=1 face="sans-serif"> cc: jdom-interest@jdom.org</font>
<br><font size=1 face="sans-serif"> Subject: Re: [jdom-interest] Thread questions regarding JDOM SAXBuiler?</font></table>
<br>
<br>
<br><font size=2 face="Courier New">Hi,<br>
<br>
I meant to make the program self-cotained but missed the dependency<br>
on the concurrent jar. Here's a new version. You should run the test<br>
in your environment to confirm the results.<br>
<br>
Yes, documents are discarded after being built. There are many variations<br>
you can do in a test like this. My guess is that it's String/StringBuffer<br>
handling in SAXBuilder and/or Xerces that accounts for the resuts.<br>
<br>
A typical output in my environment (P3, 850Mhz, Dell Latitude C600):<br>
<br>
Reuse=true size=21731 time: 5720<br>
Reuse=false size=21731 time: 2215<br>
<br>
Reuse=true size=1918 time: 200<br>
Reuse=false size=1918 time: 300<br>
<br>
Reuse=true size=21731 time: 1200<br>
Reuse=false size=21731 time: 2065<br>
<br>
Reuse=true size=43259 time: 3697<br>
Reuse=false size=43259 time: 2663<br>
<br>
Reuse=true size=324070 time: 25435<br>
Reuse=false size=324070 time: 22233<br>
<br>
Reuse=true size=756109 time: 66417<br>
Reuse=false size=756109 time: 53194<br>
<br>
The first run should be disregarded. Used for warming-up.<br>
<br>
/pmn<br>
<br>
David Wall wrote:<br>
<br>
> Peter,<br>
> <br>
> Thanks for your input. Can you share the results you got?<br>
> <br>
> Can anybody explain that behavior? It sounds suspect. Of course, the cost<br>
> of creating a SAXBuilder should go down relative to the time for parsing as<br>
> the XML file gets bigger, but the cost of construction shouldn't change much<br>
> unless there's a memory leak in the program. For example, are the Documents<br>
> created from build() being destroyed? Is it just the garbage collector<br>
> that's entering the picture? I know that the modern GC does well with lots<br>
> of small objects coming and going because that's the most typical scenario<br>
> (especially String). But it seems odd that the construction of an object<br>
> would change just because bigger XML files are used in the build() method.<br>
> <br>
<br>
package large;<br>
<br>
import java.io.StringReader;<br>
import java.text.DateFormat;<br>
import java.text.DateFormatSymbols;<br>
import java.util.Calendar;<br>
import java.util.Date;<br>
<br>
import org.jdom.Comment;<br>
import org.jdom.Document;<br>
import org.jdom.Element;<br>
import org.jdom.input.SAXBuilder;<br>
import org.jdom.output.XMLOutputter;<br>
import org.xml.sax.InputSource;<br>
<br>
/**<br>
* @author Per Norrman<br>
* <br>
*/<br>
public class ThreadedReader {<br>
private boolean _reuse = true;<br>
<br>
private String _xml = "";<br>
<br>
private long _time = 0;<br>
<br>
public ThreadedReader(boolean reuse) {<br>
_reuse = reuse;<br>
}<br>
<br>
public synchronized void addTime(long elapsed) {<br>
_time += elapsed;<br>
}<br>
<br>
public synchronized long getTime() {<br>
return _time;<br>
}<br>
<br>
public void reset() {<br>
_time = 0;<br>
}<br>
<br>
public void process(String start, String end, int count) throws Exception {<br>
reset();<br>
generate(start, end);<br>
<br>
// create threads<br>
int each = count / 5;</font>
<br><font size=2 face="Courier New"> Thread[] thread = new Thread[5];<br>
for (int i = 0; i < 5; ++i) {<br>
thread[i] = new ReaderThread(_reuse, each);<br>
thread[i].start();<br>
}<br>
<br>
for (int i = 0; i < 5; ++i) {<br>
thread[i].join();<br>
}<br>
<br>
// report<br>
System.out.println("Reuse=" + _reuse + "\tsize=" + _xml.length()<br>
+ "\ttime: " + getTime());<br>
}<br>
<br>
public void generate(String startDate, String endDate) throws Exception {<br>
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);<br>
DateFormatSymbols dfs = new DateFormatSymbols();<br>
String[] weekDays = dfs.getWeekdays();<br>
<br>
Element root = new Element("root");<br>
Document doc = new Document(root);<br>
doc.getContent().add(0,<br>
new Comment(" Generated: " + df.format(new Date()) + " "));<br>
<br>
Calendar cal = Calendar.getInstance();<br>
Date start = df.parse(startDate);<br>
Date end = df.parse(endDate);<br>
<br>
cal.setTime(start);<br>
while (cal.getTime().before(end)) {<br>
Element date = new Element("day");<br>
date.addContent(new Element("date").setText(df<br>
.format(cal.getTime())));<br>
root.addContent(date);<br>
String weekDay = weekDays[cal.get(Calendar.DAY_OF_WEEK)];<br>
Element day = new Element("dayname").setText(weekDay);<br>
date.addContent(day);<br>
cal.add(Calendar.DATE, 1);<br>
}<br>
<br>
XMLOutputter out = new XMLOutputter();<br>
<br>
_xml = out.outputString(doc);<br>
<br>
}<br>
<br>
public static void test(String start, String end) throws Exception {<br>
System.out.println();<br>
new ThreadedReader(true).process(start, end, 20);<br>
new ThreadedReader(false).process(start, end, 20);<br>
}<br>
<br>
public static void main(String[] args) throws Exception {<br>
test("2000-01-01", "2001-01-01");<br>
test("2000-01-01", "2000-02-01");<br>
test("2000-01-01", "2001-01-01");<br>
test("2000-01-01", "2001-12-31");<br>
test("1990-01-01", "2004-12-31");<br>
test("1970-01-01", "2004-12-31");<br>
}<br>
<br>
private class ReaderThread extends Thread {<br>
private boolean _reuse = true;<br>
<br>
private int _count = 0;<br>
<br>
private SAXBuilder _builder = new SAXBuilder();<br>
<br>
public ReaderThread(boolean reuse, int count) {<br>
_reuse = reuse;<br>
_count = count;<br>
_builder.setReuseParser(reuse);<br>
}<br>
<br>
private void parse(InputSource source) {<br>
long elapsed = 0;<br>
try {<br>
elapsed = System.currentTimeMillis();<br>
if (_reuse) {<br>
_builder.build(source);<br>
} else {<br>
SAXBuilder builder = new SAXBuilder();<br>
_builder.build(source);<br>
}<br>
elapsed = System.currentTimeMillis() - elapsed;<br>
addTime(elapsed);<br>
} catch (Exception e) {<br>
System.out.println(getName() + ": " + e.getMessage());<br>
}<br>
}<br>
<br>
public void run() {<br>
while (_count-- > 0) {<br>
parse(new InputSource(new StringReader(_xml)));<br>
}<br>
}<br>
}<br>
<br>
}_______________________________________________<br>
To control your jdom-interest membership:<br>
http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com</font>
<br>
<br>