测试的xml文件大小分别是003K,010K,184K,744K
VTD用com.ximpleware.vtd-xml,version is 2.13
SAX用sax.sax,version is 2.0.1
测试功能只实现了 解析,并没有向POJO去映射,困为这种POJO映射行为是统一的,没什么区别,我们单单测试 xml parsor 性能。
SumTimeMS表示解析100次的总时间,单位是毫秒
============================================
xmlSizeKB, vtdSumTimeMS,saxSumTimeMS
003K, 137MS, 192MS010K, 189MS, 237MS
184K, 454MS, 402MS
744K, 606MS, 587MS
============================================
从测试结果来看
如果文件比较小,大概010K左右,vtd性能可以提高16%左右。
如果文件比较大,大概100K左右,vtd性能开始慢了,没有sax性能好。
测试数据和测试方法,应该没什么问题
vtd号称The World Fastest XML Parsor,看来,也不怎么样。
package com.jelly; import com.ximpleware.VTDGen; import com.ximpleware.VTDNav; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import java.io.BufferedInputStream; import java.io.InputStream; public class XmlPerf { private static final String xml = "/myDoc744.xml"; public static void main(String[] args) throws Exception { long sum = 0; for(int i = 0; i < 100; i++){ long s = System.currentTimeMillis(); // vtdTest(); saxTest(); long e = System.currentTimeMillis(); sum += (e - s); } double sumD = (double)sum; System.out.println("sumMS=" + sumD + ",avgMS=" + sumD/10D); } private static void vtdTest() throws Exception { InputStream is = XmlPerf.class.getResourceAsStream(xml); BufferedInputStream bis = new BufferedInputStream(is); byte[] b = new byte[is.available()]; bis.read(b); bis.close(); VTDGen vg = new VTDGen(); vg.setDoc(b); vg.parse(false); VTDNav vn = vg.getNav(); int size = vn.getTokenCount(); for (int i = 0; i < size; i++) { vn.toString(i).trim(); } vg.clear(); } private static void saxTest() throws Exception { DefaultHandler handler = new DefaultHandler(){ @Override public void startElement(String uri,String localName,String qName,Attributes attributes) throws SAXException { // System.out.println("qName=" + qName); } @Override public void endElement(String uri,String qName) throws SAXException { // System.out.println("qName=" + qName); } @Override public void characters(char[] ch,int start,int length) throws SAXException { // System.out.println("content>>>" + new String(ByteBuffer.wrap(ch,start,length))); } }; InputStream is = XmlPerf.class.getResourceAsStream(xml); BufferedInputStream bis = new BufferedInputStream(is); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); saxParser.parse(bis,handler); bis.close(); } }原文链接:https://www.f2er.com/xml/294641.html