From jeff at kanecap.com  Fri May  6 20:45:47 2005
From: jeff at kanecap.com (Jeff Enos)
Date: Fri May  6 20:46:44 2005
Subject: [Rd] Varying as.Date performance
In-Reply-To: <Pine.LNX.4.61.0505051557100.10592@gannet.stats>
References: <17017.17895.337156.535491@gargle.gargle.HOWL>
	<Pine.LNX.4.61.0505050717100.20206@gannet.stats>
	<17018.7818.716318.388693@gargle.gargle.HOWL>
	<x2vf5xlpb2.fsf@turmalin.kubism.ku.dk>
	<Pine.LNX.4.61.0505051557100.10592@gannet.stats>
Message-ID: <17019.47963.289452.97465@gargle.gargle.HOWL>

Thanks for this optimization and for adding it to R-devel.  My
original command on the glibc 2.3.3 machine now runs much faster:

> system.time(x <- as.Date(rep("01-01-2005", 100000), format = "%m-%d-%Y"))
[1] 0.97 0.91 1.88 0.00 0.00

and is in line with the time seen on the glibc 2.3.2 machine.

Jeff

Prof Brian Ripley writes:
 > On Thu, 5 May 2005, Peter Dalgaard wrote:
 > 
 > > Jeff Enos <jeff@kanecap.com> writes:
 > >
 > >> Thanks for these suggestions.  C-level profiling yields the following:
 > >>
 > >>   %   cumulative   self              self     total
 > >>  time   seconds   seconds    calls   s/call   s/call  name
 > >>  36.01      5.34     5.34   100000     0.00     0.00  get_locale_strings
 > >>   4.32      5.98     0.64   100000     0.00     0.00  mktime00
 > >>   3.98      6.57     0.59   277462     0.00     0.00  Rf_eval
 > >>   3.71      7.12     0.55   472935     0.00     0.00  Rf_findVarInFrame3
 > >>   3.64      7.66     0.54   100000     0.00     0.00  strptime_internal
 > >>   3.51      8.18     0.52        1     0.52     7.51  do_strptime
 > >>
 > >> It looks like strftime is called from get_locale_strings, which might
 > >> be the culprit.  Any suggestions on where I might go from here?
 > >
 > > You might try modifying get_locale_strings (and its wide counterpart)
 > > with a check for an unchanged locale. E.g.
 > >
 > > static char *last_LC_TIME=NULL;
 > >
 > > ....
 > >
 > >  tmp = setlocale(LC_TIME, NULL)
 > >  if (strcmp(tmp, last_LC_TIME)) return;
 > >
 > >  last_LC_TIME = tmp;
 > >
 > > .... set the strings ....
 > >
 > > if the call to setlocale is considerably faster than 40 calls to
 > > strftime(), you might have a winner.
 > 
 > Yes, I think that would be a worthwhile optimization.  I didn't bother 
 > because I figured it would be fast enough (which at 50musec it almost 
 > always is).
 > 
 > However, get_locale_strings is only 36% of the total, and we have at least 
 > another 60% to account for.  (81.01 vs 1.18 secs.)
 > 
 > -- 
 > Brian D. Ripley,                  ripley@stats.ox.ac.uk
 > Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
 > University of Oxford,             Tel:  +44 1865 272861 (self)
 > 1 South Parks Road,                     +44 1865 272866 (PA)
 > Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-- 
Jeff Enos
Kane Capital Management
jeff@kanecap.com


[Rd] Varying as.Date performance
Jeff Enos