dom4j生成xml以及StringBuffer生成xml性能测试

public class T {

	public static void main(String[] args) {
		long a = System.currentTimeMillis();
		
		T.fun_1(200000);//781
//		T.fun_2(200000);//110
		
		System.out.println("耗时:"+(System.currentTimeMillis()-a));
	}
	public static String fun_2(int count){
		StringBuffer sbStr = new StringBuffer();
		sbStr.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?> ");
		sbStr.append("<Response>");
		for(int i=0;i<count;i++){
			sbStr.append("<Data>"+i+"</Data>");
		}
		sbStr.append("</Response>");
		return sbStr.toString();
	}
	public static String fun_1(int count){
		Document doc = DocumentHelper.createDocument();
		Element response_E = DocumentHelper.createElement("Response");
		Element data_E = null;
		for(int i=0;i<count;i++){
			 data_E = DocumentHelper.createElement("Data");
			 data_E.setText(""+i);
			 response_E.add(data_E);
		}
		doc.add(response_E);
		return doc.asXML();
		
	}
}

方法1需要781这毫秒(30万条时,内存不足)

方法2需要110毫秒(60万条时内存不足)

你可能感兴趣的:(StringBuffer)