Re: Formatting a date

Alex Twisleton-Wykeham-Fiennes <[email protected]> Mon, 27 Mar 2006 22:39:42 +0100
Newsgroups gmane.comp.java.webmacro.user
Message-ID <[email protected]>
--Boundary-00=_euFKEzfny9k2Z/T
Content-Type: text/plain;
  charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

On Mon 27 March 2006 21:51, Keats Kirsch wrote:
> Endre St=F8lsvik wrote:
> >| I'll also look at making $Text.formatDate cache the SimpleTextFormat
> >| objects that it creates and run some profiling to see whether or not
> >| this is really an advantage...
> >
> >And where should this cache reside? In which context? Who's gonna clean
> >it? When? How? (And how do I reload my webapp in Tomcat?)
> >
> >Caches on every corner, under every rock, in every creek, and in other
> >unknown places, is the way to evil code.
>
> I think your lyrical rejoinder is a bit overstretched.  Sometimes
> caching is good, sometimes it isn't.  In this case we are talking about
> caching a small number or small items, which may be a performance win.
>
> Notice that Alex said he would do some testing to see if this is
> worthwhile.  It may not be.  We've been reducing the amount of explicit
> caching in WM as JVMs have become much better at doing this for us.
> It's difficult to predict when you will see a payoff without testing.

attached is very crude test case that compares SimpleDateFormat creation=20
versus SimpleDateFormat caching using the example formats on the=20
SimpleDateFormat javadoc.  Output is as follows:-

creating: 4027
caching: 1279
synchronized: 1287

So we have a rough 3 or 4 x speed up when caching the value across 100000=20
iterations.

The last entry is doing exactly the same cached lookup as the second entry =
on=20
the same cache with a synchronized lock around the HashMap.  I throw this i=
n=20
purely to illustrate that the performance overload of synchronization is no=
t=20
going cripple your code.

Alex

> In this case the cache could just be a static map inside of the factory
> class.  This optional overhead would be minimal and not interfere with
> Servlet reloading in any way.
>
> Keats
>
> >Regards,
> >Endre.
>
> -------------------------------------------------------
> This SF.Net email is sponsored by xPML, a groundbreaking scripting langua=
ge
> that extends applications into web and mobile media. Attend the live
> webcast and join the prime developer group breaking into this new coding
> territory!
> http://sel.as-us.falkag.net/sel?cmd=3Dlnk&kid=3D110944&bid=3D241720&dat=
=3D121642
> _______________________________________________
> Webmacro-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/webmacro-user

--Boundary-00=_euFKEzfny9k2Z/T
Content-Type: text/x-java;
  charset="iso-8859-1";
  name="SimpleDateFormatterTest.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="SimpleDateFormatterTest.java"

import java.util.*;
import java.text.*;

public class SimpleDateFormatterTest 
{
  public final static String[]  formats = new String[] { "yyyy.MM.dd G 'at' HH:mm:ss z",
                                                         "EEE, MMM d, ''yy",
                                                         "h:mm a",
                                                         "hh 'o''clock' a, zzzz",
                                                         "K:mm a, z",
                                                         "yyyyy.MMMMM.dd GGG hh:mm aaa",
                                                         "EEE, d MMM yyyy HH:mm:ss Z",
                                                         "yyMMddHHmmssZ" };

  public final static String[] keys = new String[formats.length];

  public static void main(String[] args) 
  {
    HashMap sdfCache = new HashMap();
    for (int i = 0; i < formats.length; i++) {
      String key = Integer.toString(i);
      sdfCache.put(key, new SimpleDateFormat(formats[i]));
      keys[i] = key;
    }
    long now = System.currentTimeMillis();
    for (int i = 0; i < 100000; i++) {
      SimpleDateFormat sdf = new SimpleDateFormat(formats[i % formats.length]);
      sdf.format(new Date());
    }
    System.out.println("creating: " + (System.currentTimeMillis() - now));
    now = System.currentTimeMillis();
    for (int i = 0; i < 100000; i++) {
      SimpleDateFormat sdf = (SimpleDateFormat) sdfCache.get(keys[i % formats.length]);
      sdf.format(new Date());
    }
    System.out.println("caching: " + (System.currentTimeMillis() - now));
    now = System.currentTimeMillis();
    for (int i = 0; i < 100000; i++) {
      SimpleDateFormat sdf = null;
      synchronized (sdfCache) {
         sdf = (SimpleDateFormat) sdfCache.get(keys[i % formats.length]);
      }
      sdf.format(new Date());
    }
    System.out.println("synchronized: " + (System.currentTimeMillis() - now));
  }
}
--Boundary-00=_euFKEzfny9k2Z/T--


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642