KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JFlex > Timer


1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * JFlex 1.4.1 *
3  * Copyright (C) 1998-2004 Gerwin Klein <lsf@jflex.de> *
4  * All rights reserved. *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License. See the file *
8  * COPYRIGHT for more information. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License along *
16  * with this program; if not, write to the Free Software Foundation, Inc., *
17  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
18  * *
19  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

20 package JFlex;
21
22 /**
23  * Very simple timer for code generation time statistics.
24  *
25  * Not very exact, measures user time, not processor time.
26  *
27  * @author Gerwin Klein
28  * @version JFlex 1.4.1, $Revision: 2.3 $, $Date: 2004/11/06 23:03:32 $
29  */

30 public class Timer {
31
32   /* the timer stores start and stop time from currentTimeMillis() */
33   private long startTime, stopTime;
34
35   /* flag if the timer is running (if stop time is valid) */
36   private boolean running;
37
38
39   /**
40    * Construct a new timer that starts immediatly.
41    */

42   public Timer() {
43     startTime = System.currentTimeMillis();
44     running = true;
45   }
46
47   
48   /**
49    * Start the timer. If it is already running, the old start
50    * time is lost.
51    */

52   public void start() {
53     startTime = System.currentTimeMillis();
54     running = true;
55   }
56
57
58   /**
59    * Stop the timer.
60    */

61   public void stop() {
62     stopTime = System.currentTimeMillis();
63     running = false;
64   }
65
66   
67   /**
68    * Return the number of milliseconds the timer has been running.
69    *
70    * (up till now, if it still runs, up to the stop time if it has been stopped)
71    */

72   public long diff() {
73     if (running)
74       return System.currentTimeMillis()-startTime;
75     else
76       return stopTime-startTime;
77   }
78
79   
80   /**
81    * Return a string representation of the timer.
82    *
83    * @return a string displaying the diff-time in readable format (h m s ms)
84    *
85    * @see Timer#diff
86    */

87   public String JavaDoc toString() {
88     long diff = diff();
89     
90     long millis = diff%1000;
91     long secs = (diff/1000)%60;
92     long mins = (diff/(1000*60))%60;
93     long hs = (diff/(1000*3600))%24;
94     long days = diff/(1000*3600*24);
95
96     if (days > 0)
97       return days+"d "+hs+"h "+mins+"m "+secs+"s "+millis+"ms";
98
99     if (hs > 0)
100       return hs+"h "+mins+"m "+secs+"s "+millis+"ms";
101
102     if (mins > 0)
103       return mins+"m "+secs+"s "+millis+"ms";
104
105     if (secs > 0)
106       return secs+"s "+millis+"ms";
107
108     return millis+"ms";
109   }
110 }
111
Popular Tags