给下边的代码生成执行耗时统计:public class BookstoreBuilder { public Bookstore parseDocument(InputStream inputStream) { Bookstore bookstore = new Bookstore(); try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() { private BookBuilder bookBuilder; private String currentElement; @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { switch (qName) { case "book": bookBuilder = new BookBuilder(); bookBuilder.setCategory(attributes.getValue("category")); break; case "title": case "author": case "year": case "price": currentElement = qName; break; } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { switch (qName) { case "book": bookstore.addBook(bookBuilder.build()); bookBuilder = null; break; case "title": case "author": case "year": case "price": currentElement = null; break; } } @Override public void characters(char[] ch, int start, int length) throws SAXException { if (currentElement != null) { String value = new String(ch, start, length); switch (currentElement) { case "title": bookBuilder.setTitle(value); break; case "author": bookBuilder.setAuthor(value); break; case "year": bookBuilder.setYear(Integer.parseInt(value)); break; case "price": bookBuilder.setPrice(Double.parseDouble(value)); break; } } } }; saxParser.parse(inputStream, handler); } catch (ParserConfigurationException | SAXException | IOException e) { e.printStackTrace(); } return bookstore; } }
时间: 2023-06-26 19:07:12 浏览: 95
JavaEE源代码 bookstore
可以使用Java自带的System.nanoTime()方法来计算代码执行时间,计算方法如下:
public class BookstoreBuilder {
public Bookstore parseDocument(InputStream inputStream) {
Bookstore bookstore = new Bookstore();
long startTime = System.nanoTime(); // 开始时间
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
private BookBuilder bookBuilder;
private String currentElement;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
switch (qName) {
case "book":
bookBuilder = new BookBuilder();
bookBuilder.setCategory(attributes.getValue("category"));
break;
case "title":
case "author":
case "year":
case "price":
currentElement = qName;
break;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
switch (qName) {
case "book":
bookstore.addBook(bookBuilder.build());
bookBuilder = null;
break;
case "title":
case "author":
case "year":
case "price":
currentElement = null;
break;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (currentElement != null) {
String value = new String(ch, start, length);
switch (currentElement) {
case "title":
bookBuilder.setTitle(value);
break;
case "author":
bookBuilder.setAuthor(value);
break;
case "year":
bookBuilder.setYear(Integer.parseInt(value));
break;
case "price":
bookBuilder.setPrice(Double.parseDouble(value));
break;
}
}
}
};
saxParser.parse(inputStream, handler);
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
long endTime = System.nanoTime(); // 结束时间
System.out.println("执行耗时:" + (endTime - startTime) + "ns"); // 输出执行时间
return bookstore;
}
}
阅读全文