Recently, I am very concerned about XML and the efficiency of various popular parsing technologies. when testing the running time, I encountered some difficulties. Let's take a look at my questions.
-----------------------------------------------------------------------------
In a class, a method called test () needs to be called by the JSP end (the main task of test () is system. out. print (), so Add "long lasting = system. currenttimemillis (), "add" system. out. println ("Run time:" + (system. currenttimemillis ()-Lasting) + "millisecond ")".
Then, change the test () method to the main () method to test the class separately, and adjust the JSP code to access the main () method. The strange thing happened. After repeated tests, it took 451 milliseconds to access the main () method using JSP, and 2864 milliseconds to run this class separately.
Why? Is it because of Web containers?
-----------------------------------------------------------------------------
On a forum where I was very angry, I got some answers and summarized them myself. Please come to your comments:
When JSP is requested for the first time, the Web Container sends JSP to the compiler and compiles it into a servlet. Then, the servlet and other classes (such as self-written classes) are cached in the Web Container, stored in the memory, and the response results are returned to the client. All subsequent requests of JSP are, the Web Container will automatically load the cache. Unless the JSP or class has been modified, the Web Container will not re-compile and construct the cache. The separate running classes are different. Because there is no Web Container cache support, it takes a little longer to re-compile each operation and read it into the memory.
In addition, according to the test, when the JSP end is called for the first time, the time is similar to that of the separately run class. This also confirms the above explanation.
Add source program
Class:
Package com. test;
Import org. xml. Sax .*;
Import org. xml. Sax. helpers .*;
Import javax. xml. parsers .*;
Public class myxmlreader extends defaulthandler {
Java. util. Stack tags = new java. util. Stack ();
Public myxmlreader (){
Super ();
}
Public static void main (string ARGs []) {
Long Lasting = system. currenttimemillis ();
Try {
Saxparserfactory Sf = saxparserfactory. newinstance ();
Saxparser sp = SF. newsaxparser ();
Myxmlreader reader = new myxmlreader ();
Sp. parse (New inputsource ("data. xml"), Reader );
}
Catch (exception e ){
E. printstacktrace ();
}
System. Out. println ("Run time:" + (system. currenttimemillis ()-Lasting) + "millisecond ");
}
Public void characters (char ch [], int start, int length)
Throws saxexception
{
// Obtain information about the current node from the stack
String tag = (string) tags. Peek ();
If (tag. Equals ("no ")){
System. Out. Print ("license plate number:" + new string (CH, start, length ));
}
If (tag. Equals ("ADDR ")){
System. Out. println ("Address:" + new string (CH, start, length ));
}
}
Public void startelement (string Uri, string localname, string QNAME, attributes attrs ){
Tags. Push (QNAME );
}
}
JSP:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "com. Test. *" %>
<%
Long Lasting = system. currenttimemillis ();
%>
<HTML>
<Body>
<%
String ARGs [] = {""};
Myxmlreader. Main (ARGs );
%>
</Body>
</Html>
(Please note! This document shall indicate the original author Rosen Jiang and its source:Http://blog.csdn.net/rosen)