logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Cakalic, James" <James.Caka...@heybridge.com>
Subject RE: [PATCH] ISO8601DateFormat/%d{ISO8601} speed optimization
Date Fri, 18 Jan 2002 22:12:09 GMT
ISO8601 allows a lot of variation in date/time formats dependent on the
specific application. On the point of decimal separator for the fractional
part of a time the spec states:

"If necessary for a particular application a decimal fraction of hour,
minute or second may be included. If a decimal fraction is included, lower
order components (if any) shall be omitted and the decimal fraction shall be
divided from the integer part by the decimal sign specified in ISO 31-0:
i.e. the comma [,] or full stop [.]. Of these, the comma is the preferred
sign."

Since the specification clearly stated a "preference" for the decimal
fraction separator, we had chosen to use a comma some time ago and not the
local-preferred decimal separator. Although the decision is not up to me
(and this is clearly a minor point and seem a bit nit-picking), I would
encourage following the preference stated in the specification in this area.

Of course, that's must my opinion. I could be wrong.

Best regards,
Jim Cakalic

> -----Original Message-----
> From: Andrew Vajoczki [mailto:vajoczki@yahoo.com]
> Sent: Friday, January 18, 2002 12:05 PM
> To: Log4J Developers List
> Subject: [PATCH] ISO8601DateFormat/%d{ISO8601} speed optimization
> 
> 
> Overall Log4J logging speed with typical ISO date format strings
> (like "%p %t %d{ISO8601} %c %m%n") is roughly 50% faster 
> with this date string caching optimization (at least on
> my JDK 1.3/Windows machine).
> 
> This puts typical logging using %d{ISO8601} closer to 
> as %r (relative) timings.
> 
> Although I've tested it and it seems fine, more testing never hurts.
> 
> diff -u made against
> jakarta-log4j-1.2alpha6/src/java/org/apache/log4j/helpers/ISO8
601DateFormat.java
> 
> -Andrew Vajoczki
> 
> --- ISO8601DateFormat.java.orig	Thu Jan 17 19:52:28 2002
> +++ ISO8601DateFormat.java	Fri Jan 18 12:20:26 2002
> @@ -41,6 +41,10 @@
>    ISO8601DateFormat(TimeZone timeZone) {
>      super(timeZone);
>    }
> +
> +  static private Object lastTimeLock = new Object();;
> +  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,87 @@
>    StringBuffer format(Date date, StringBuffer sbuf,
>  		      FieldPosition fieldPosition) {
>  
> -    calendar.setTime(date);      
> -
> -    int year =  calendar.get(Calendar.YEAR);
> -    sbuf.append(year);
> +    long now = date.getTime();
> +    int millis = (int)(now % 1000);
>  
> -    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;
> +    synchronized (lastTimeLock) {
> +      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);
> +
> +        // use current locale's decimal separator
> +        sbuf.append(new 
> java.text.DecimalFormatSymbols().getDecimalSeparator());
> +        
> +        // store the time string for next time to avoid recomputation
> +        sbuf.getChars(start, sbuf.length(), lastTimeString, 0);
> +        lastTime = now - millis;
> +      } 
> +      else {
> +        sbuf.append(lastTimeString);
> +      } 
>      }
> -    sbuf.append(month);
> -
> -    int day = calendar.get(Calendar.DAY_OF_MONTH);
> -    if(day < 10) 
> -      sbuf.append('0');
> -    sbuf.append(day);
> -
> -    sbuf.append(' ');    
> -    return super.format(date, sbuf, fieldPosition);
> -  }
> +    
> +    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 +146,4 @@
>      return null;
>    }  
>  }
> +
> 


<font size="1">Confidentiality Warning:  This e-mail contains information intended only
for the use of the individual or entity named above.  If the reader of this e-mail is not
the intended recipient or the employee or agent responsible for delivering it to the intended
recipient, any dissemination, publication or copying of this e-mail is strictly prohibited.
The sender does not accept any responsibility for any loss, disruption or damage to your data
or computer system that may occur while using data contained in, or transmitted with, this
e-mail.   If you have received this e-mail in error, please immediately notify us by return
e-mail.  Thank you.


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message