You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-cvs@jakarta.apache.org by ce...@apache.org on 2002/02/17 22:50:58 UTC
cvs commit: jakarta-log4j/src/java/org/apache/log4j/helpers AbsoluteTimeDateFormat.java ISO8601DateFormat.java
ceki 02/02/17 13:50:58
Modified: src/java/org/apache/log4j/helpers
AbsoluteTimeDateFormat.java ISO8601DateFormat.java
Log:
- Incorporated the performance enhancements to ISO8601DateFormat and
AbsoluteTimeDateFormat classes submitted by Andrew Vajoczki.
Submitted by: Andrew Vajoczki
Reviewed by: Ceki
Revision Changes Path
1.4 +46 -26 jakarta-log4j/src/java/org/apache/log4j/helpers/AbsoluteTimeDateFormat.java
Index: AbsoluteTimeDateFormat.java
===================================================================
RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/helpers/AbsoluteTimeDateFormat.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- AbsoluteTimeDateFormat.java 5 Jul 2001 19:09:33 -0000 1.3
+++ AbsoluteTimeDateFormat.java 17 Feb 2002 21:50:58 -0000 1.4
@@ -14,15 +14,14 @@
import java.text.ParsePosition;
import java.text.DateFormat;
-
/**
-
Formats a {@link Date} in the format "HH:mm:ss,SSS" for example,
"15:49:37,459".
@author Ceki Gülcü
-
+ @author Andrew Vajoczki
+
@since 0.7.5
*/
public class AbsoluteTimeDateFormat extends DateFormat {
@@ -56,7 +55,10 @@
AbsoluteTimeDateFormat(TimeZone timeZone) {
setCalendar(Calendar.getInstance(timeZone));
}
-
+
+ private static long previousTime;
+ private static char[] previousTimeWithoutMillis = new char[9]; // "HH:mm:ss."
+
/**
Appends to <code>sbuf</code> the time in the format
"HH:mm:ss,SSS" for example, "15:49:37,459"
@@ -69,32 +71,50 @@
StringBuffer format(Date date, StringBuffer sbuf,
FieldPosition fieldPosition) {
- // We use a previously instantiated Date object to avoid the needless
- // creation of temporary objects. This saves a few micro-secs.
- calendar.setTime(date);
-
- int hour = calendar.get(Calendar.HOUR_OF_DAY);
- if(hour < 10) {
- sbuf.append('0');
- }
- sbuf.append(hour);
- sbuf.append(':');
+ long now = date.getTime();
+ int millis = (int)(now % 1000);
- int mins = calendar.get(Calendar.MINUTE);
- if(mins < 10) {
- sbuf.append('0');
+ if ((now - millis) != previousTime) {
+ // We reach this point at most once per second
+ // across all threads instead of each time format()
+ // is called. This saves considerable CPU time.
+
+ calendar.setTime(date);
+
+ int start = sbuf.length();
+
+ int hour = calendar.get(Calendar.HOUR_OF_DAY);
+ if(hour < 10) {
+ sbuf.append('0');
+ }
+ sbuf.append(hour);
+ sbuf.append(':');
+
+ int mins = calendar.get(Calendar.MINUTE);
+ if(mins < 10) {
+ sbuf.append('0');
+ }
+ sbuf.append(mins);
+ sbuf.append(':');
+
+ int secs = calendar.get(Calendar.SECOND);
+ if(secs < 10) {
+ sbuf.append('0');
+ }
+ sbuf.append(secs);
+ sbuf.append(',');
+
+ // store the time string for next time to avoid recomputation
+ sbuf.getChars(start, sbuf.length(), previousTimeWithoutMillis, 0);
+
+ previousTime = now - millis;
}
- sbuf.append(mins);
- sbuf.append(':');
-
- int secs = calendar.get(Calendar.SECOND);
- if(secs < 10) {
- sbuf.append('0');
+ else {
+ sbuf.append(previousTimeWithoutMillis);
}
- sbuf.append(secs);
- sbuf.append(',');
- int millis = calendar.get(Calendar.MILLISECOND);
+
+
if(millis < 100)
sbuf.append('0');
if(millis < 10)
1.5 +83 -30 jakarta-log4j/src/java/org/apache/log4j/helpers/ISO8601DateFormat.java
Index: ISO8601DateFormat.java
===================================================================
RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/helpers/ISO8601DateFormat.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- ISO8601DateFormat.java 5 Jul 2001 19:09:39 -0000 1.4
+++ ISO8601DateFormat.java 17 Feb 2002 21:50:58 -0000 1.5
@@ -28,7 +28,8 @@
information on this format.
@author Ceki Gülcü
-
+ @author Andrew Vajoczki
+
@since 0.7.5
*/
public class ISO8601DateFormat extends AbsoluteTimeDateFormat {
@@ -41,6 +42,9 @@
ISO8601DateFormat(TimeZone timeZone) {
super(timeZone);
}
+
+ static private long lastTime;
+ static private char[] lastTimeString = new char[20];
/**
Appends a date in the format "YYYY-mm-dd HH:mm:ss,SSS"
@@ -52,37 +56,85 @@
StringBuffer format(Date date, StringBuffer sbuf,
FieldPosition fieldPosition) {
- calendar.setTime(date);
+ long now = date.getTime();
+ int millis = (int)(now % 1000);
- int year = calendar.get(Calendar.YEAR);
- sbuf.append(year);
-
- String month;
- switch(calendar.get(Calendar.MONTH)) {
- case Calendar.JANUARY: month = "-01-"; break;
- case Calendar.FEBRUARY: month = "-02-"; break;
- case Calendar.MARCH: month = "-03-"; break;
- case Calendar.APRIL: month = "-04-"; break;
- case Calendar.MAY: month = "-05-"; break;
- case Calendar.JUNE: month = "-06-"; break;
- case Calendar.JULY: month = "-07-"; break;
- case Calendar.AUGUST: month = "-08-"; break;
- case Calendar.SEPTEMBER: month = "-09-"; break;
- case Calendar.OCTOBER: month = "-10-"; break;
- case Calendar.NOVEMBER: month = "-11-"; break;
- case Calendar.DECEMBER: month = "-12-"; break;
- default: month = "-NA-"; break;
- }
- sbuf.append(month);
-
- int day = calendar.get(Calendar.DAY_OF_MONTH);
- if(day < 10)
- sbuf.append('0');
- sbuf.append(day);
+ if ((now - millis) != lastTime) {
+ // We reach this point at most once per second
+ // across all threads instead of each time format()
+ // is called. This saves considerable CPU time.
+
+ calendar.setTime(date);
+
+ int start = sbuf.length();
+
+ int year = calendar.get(Calendar.YEAR);
+ sbuf.append(year);
+
+ String month;
+ switch(calendar.get(Calendar.MONTH)) {
+ case Calendar.JANUARY: month = "-01-"; break;
+ case Calendar.FEBRUARY: month = "-02-"; break;
+ case Calendar.MARCH: month = "-03-"; break;
+ case Calendar.APRIL: month = "-04-"; break;
+ case Calendar.MAY: month = "-05-"; break;
+ case Calendar.JUNE: month = "-06-"; break;
+ case Calendar.JULY: month = "-07-"; break;
+ case Calendar.AUGUST: month = "-08-"; break;
+ case Calendar.SEPTEMBER: month = "-09-"; break;
+ case Calendar.OCTOBER: month = "-10-"; break;
+ case Calendar.NOVEMBER: month = "-11-"; break;
+ case Calendar.DECEMBER: month = "-12-"; break;
+ default: month = "-NA-"; break;
+ }
+ sbuf.append(month);
+
+ int day = calendar.get(Calendar.DAY_OF_MONTH);
+ if(day < 10)
+ sbuf.append('0');
+ sbuf.append(day);
+
+ sbuf.append(' ');
+
+ int hour = calendar.get(Calendar.HOUR_OF_DAY);
+ if(hour < 10) {
+ sbuf.append('0');
+ }
+ sbuf.append(hour);
+ sbuf.append(':');
+
+ int mins = calendar.get(Calendar.MINUTE);
+ if(mins < 10) {
+ sbuf.append('0');
+ }
+ sbuf.append(mins);
+ sbuf.append(':');
+
+ int secs = calendar.get(Calendar.SECOND);
+ if(secs < 10) {
+ sbuf.append('0');
+ }
+ sbuf.append(secs);
- sbuf.append(' ');
- return super.format(date, sbuf, fieldPosition);
- }
+ sbuf.append(',');
+
+ // store the time string for next time to avoid recomputation
+ sbuf.getChars(start, sbuf.length(), lastTimeString, 0);
+ lastTime = now - millis;
+ }
+ else {
+ sbuf.append(lastTimeString);
+ }
+
+
+ if (millis < 100)
+ sbuf.append('0');
+ if (millis < 10)
+ sbuf.append('0');
+
+ sbuf.append(millis);
+ return sbuf;
+ }
/**
This method does not do anything but return <code>null</code>.
@@ -92,3 +144,4 @@
return null;
}
}
+
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>