2013-05-03 68 views
2

我正在嘗試測量StAX和DOM創建兩個相同文檔所需的時間。我不知道爲什麼DOM在創建XML時更快。也許我的StAX編寫器的代碼不太好。 所以這裏是 的StAX(長碼)爲什麼StAX創建XML比DOM慢?

public static final int pocet =100000; 

try { 

    String encoding = "UTF-8"; 
    XMLOutputFactory f = XMLOutputFactory.newInstance(); 
    XMLStreamWriter w = f.createXMLStreamWriter(
         new FileOutputStream(subor), 
         encoding); 
    w.writeStartDocument(encoding, "1.0"); 
    w.writeCharacters("\r\n"); 
    w.writeStartElement("Noviny");   
    for (int i = 1; i <= pocet; i++) { 
    w.writeCharacters("\r\n "); 
    w.writeStartElement("Autor"); 
    w.writeCharacters("\r\n "); 
    w.writeStartElement("Id"); 
    String ID = Integer.toString(i); 
    w.writeCharacters(ID); 
    w.writeEndElement(); 
    w.writeCharacters("\r\n "); 
    w.writeStartElement("Meno"); 
    w.writeCharacters("Autor"+i); 
    w.writeEndElement(); 
    w.writeCharacters("\r\n "); 
    w.writeStartElement("Email"); 
    w.writeCharacters("Autor"+i+"@email.com"); 
    w.writeEndElement(); 
    w.writeCharacters("\r\n "); 
    w.writeStartElement("Tel_cislo"); 
    w.writeAttribute("typ", "pevna"); 
    w.writeCharacters("+4219"); 
    w.writeEndElement(); 
    w.writeCharacters("\r\n "); 
    w.writeStartElement("plat"); 

    w.writeCharacters("5000"); 
    w.writeEndElement(); 
    w.writeCharacters("\r\n ");   
    w.writeEndElement(); 
    w.writeCharacters("\r\n"); 



    } 
    w.writeCharacters("\r\n"); 
    w.writeEndElement();   
    w.writeCharacters("\r\n"); 
    w.writeEndDocument(); 
    w.close(); 



} 
catch (Exception e) { 
    e.printStackTrace(); 
} 

DOM

 int pocet =1500000; 
try { 
DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder builder = dfactory.newDocumentBuilder(); 
DOMImplementation domImpl = builder.getDOMImplementation(); 
Document document = domImpl.createDocument(null, "Noviny", null); 
Node noviny = document.getDocumentElement(); 
for(int i = 0; i<pocet ; i++) { 
Element autor = document.createElement("autor"); 
Element meno = document.createElement("meno"); 
Element id = document.createElement("id"); 
Element email = document.createElement("email"); 
Element tel = document.createElement("tel_cislo"); 
    Element plat = document.createElement("plat"); 
Text textid = document.createTextNode(""+i); 
Text textmeno = document.createTextNode("Autor"+i); 
Text textemail = document.createTextNode("[email protected]"); 
Text texttel = document.createTextNode("65456465465"); 
Text textplat = document.createTextNode("200"); 
    noviny.appendChild(autor); 
    autor.appendChild(id); 
     id.appendChild(textid); 
    autor.appendChild(meno); 
     meno.appendChild(textmeno); 
    autor.appendChild(email); 
     email.appendChild(textemail); 
    autor.appendChild(tel); 
     tel.appendChild(texttel); 
    autor.appendChild(plat); 
     plat.appendChild(textplat); 
    } 
    TransformerFactory factory = TransformerFactory.newInstance(); 
    Transformer transformer = factory.newTransformer(); 
    transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); 
    transformer.transform(new DOMSource(document),new StreamResult(new File(subor))); 

我知道StAX的是在閱讀文件的方式快,但我不能解釋這一點。除了我有StAX的非常糟糕的代碼

感謝

+0

,我建議將問題標題改爲像「爲什麼DOM比StAX創建XML更快?」進行一些速度測量也是一件好事。 – 2013-05-03 08:38:22

+0

@Evgeniy:如果這不是問題,我會在後期發佈結果。謝謝 – ivanz 2013-05-03 09:07:39

回答

8

添加BufferedOuputStream這裏

XMLStreamWriter w = f.createXMLStreamWriter(new BufferedOutputStream(new FileOutputStream(subor)), "UTF-8"); 

現在測試的速度,你會看到,StAX的至少快2倍