A timing class

Source: Internet
Author: User

 


Package sony. utils;

Import java. util .*;
Import javax. servlet. jsp .*;
/**
* A timing class.
* Creation date: (13:09:38)
* Author: SonyMusic (sonymusic@china.com)
*/
Public class Timing {
Private ArrayList nameArray = new ArrayList ();
Private ArrayList timeArray = new ArrayList ();
Private javax. servlet. jsp. JspWriter out;
Private boolean inJsp = false;
/**
* This constructor is used in JAVA.
*/
Public Timing (){
Super ();
This. inJsp = false;
}
/**
* Timing constructor is used in JSP.
*/
Public Timing (javax. servlet. jsp. PageContext pageContext ){
Super ();
This. out = pageContext. getOut ();
This. inJsp = true;
}
/**
* The insert method is described here.
* Creation date: (13:40:36)
* @ Param name java. lang. String
*/
Public void add (String name ){
If (name = null // name. length () = 0) name = "Default ";
// If (name. length ()> 7) name = name. substring (0, 7 );
NameArray. add (name );
TimeArray. add (Calendar. getInstance ());
}
/**
* This is used in JAVA.
* Creation date: (14:39:43)
* @ Param args java. lang. String []
*/
Public static void main (String [] args ){
Timing time = new Timing ();
Time. add ("Start ");
Random ran = new Random ();
Int I = 0, j = 0;
For (I = 0; I <10000000; I ++)
J + = ran. nextInt ();
Time. add ("After 100 ");
For (I = 0; I <100000; I ++)
J + = ran. nextInt ();
Time. add ("After 100000 ");
For (I = 0; I <100000; I ++)
J + = ran. nextInt ();
Time. add ("After 100000 ");
Time. show ();
}
/**
* The insert method is described here.
* Creation date: (13:43:24)
*/
Public void show (){
If (inJsp)
ShowInJsp ();
Else
ShowInJava ();

}
/**
* The insert method is described here.
* Creation date: (13:46:06)
*/
Private void showInJava (){
If (nameArray. isEmpty ()){
System. out. println ("No time point has been added! ");
Return;
}
Calendar oldCal = null, cal = null, firstCal;
// = Calendar. getInstance ()
String name;
FirstCal = (Calendar) timeArray. get (0 );
System. out. println ("serial number name time (seconds )");
For (int I = 0; I <nameArray. size (); I ++ ){
Name = (String) nameArray. get (I );
Cal = (Calendar) timeArray. get (I );
System. out. print (I + 1 );
System. out. print ("");
System. out. print (name + "");
System. out. print (cal. get (Calendar. HOUR_OF_DAY) + ":" + cal. get (Calendar. MINUTE) + ":" + cal. get (Calendar. SECOND) + ". "+ cal. get (Calendar. MILLISECOND ));
System. out. print ("");
If (oldCal = null ){
System. out. println ("NONE ");
}
Else {
Long diffTime = cal. getTime (). getTime ()-oldCal. getTime (). getTime ();
System. out. println (double) diffTime/1000 );
}
OldCal = cal;
}
System. out. println ("");
If (nameArray. size () = 1 ){
System. out. println ("only one time point is added and the time cannot be counted! ");
}
Else {
Long diffTime = oldCal. getTime (). getTime ()-firstCal. getTime (). getTime ();
System. out. println ("Total time consumption:" + (double) diffTime/1000 + "seconds ");
}
}
/**
* The insert method is described here.
* Creation date: (13:57:56)
*/
Private void showInJsp (){
Try {
If (nameArray. isEmpty ()){
Return;
}
Calendar oldCal = null, cal = null, firstCal;
String name;
Long diffTime;
FirstCal = (Calendar) timeArray. get (0 );
System. out. println ("serial number name time (seconds )");
String str = "";
Str + = "<table width =" 400 "border =" 1 "cellspacing =" 0 "cellpadding =" 0 "> ";
Str + = "<tr> <td> NO. </td> <td> name </td> <td> time consumed (in seconds) </td> </tr> ";
For (int I = 0; I <nameArray. size (); I ++ ){
Str + = "<tr> ";
Name = (String) nameArray. get (I );
Cal = (Calendar) timeArray. get (I );
Str + = "<td> ";
Str + = I + 1;
Str + = "</td> ";
Str + = "<td>" + name + "</td> ";
Str + = "<td>"
+ Cal. get (Calendar. HOUR_OF_DAY)
+ ":"
+ Cal. get (Calendar. MINUTE)
+ ":"
+ Cal. get (Calendar. SECOND)
+ "."
+ Cal. get (Calendar. MILLISECOND)
+ "</Td> ";
If (oldCal = null ){
// System. out. println ("NONE ");
Str + = "<td> none </td> ";
} Else {
DiffTime = cal. getTime (). getTime ()-oldCal. getTime (). getTime ();
Str + = "<td>" + (double) diffTime/1000 + "</td> ";
}
OldCal = cal;
Str + = "</tr> ";
}
Str + = "<tr> <td colspan =" 4 "> ";

If (nameArray. size () = 1 ){
Str + = "only one time point is added and time cannot be counted! ";
} Else {
DiffTime = oldCal. getTime (). getTime ()-firstCal. getTime (). getTime ();

Str + = "Total time consumed:" + (double) diffTime/1000 + "seconds ";
}
Str + = "</td> </tr> ";
Str + = "</table> ";
Out. println (str );
} Catch (java. io. IOException e ){
}

}
}


The usage in JSP is as follows:

<% @ Page contentType = "text/html; charset = gb2312" %>
<% @ Page import = "sony. utils. *, java. util. *" %>
<Html>
<Head>
<Title> Untitled </title>
</Head>

<Body>
<%
Timing time = new Timing (pageContext );
Time. add ("Start ");
Random ran = new Random ();
Int I = 0, j = 0;
For (I = 0; I <1000000; I ++)
J + = ran. nextInt ();

Time. add ("After 100 ");
For (I = 0; I <100000; I ++)
J + = ran. nextInt ();
Time. add ("After 100000 ");
For (I = 0; I <100000; I ++)
J + = ran. nextInt ();
Time. add ("After 100000 ");

Time. show ();
%>
</Body>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.