On Wed, Aug 11, 2010 at 09:28, Chas. Owens <[email protected]> wrote:
> On Wed, Aug 11, 2010 at 08:39,  <[email protected]> wrote:
> snip
>> I haven't profiled it as Chas suggested, but I suspect the
>> issue is due to the difference in perl versions.
>>
>> 5.8.8 on Linux
>> 5.10.0 on Windows.
>>
>> My theory is that 5.10.x implemented some optimizations
>> that improved the speed of strftime.
> snip
>
> Unlikely, I don't remember any big changes to strftime (it is
> implemented by Perl_my_strftime in [util.c][0] if you want to check).
snip

I think what you are seeing is the penalty that strftime takes for
normalizing the data handed to it.  For instance, this code prints
today's date and the date 60 days in the future.

#!/usr/bin/perl

use strict;
use warnings;

use POSIX qw/strftime/;

my @t           = (0) x 3;
my ($d, $m, $y) = (localtime)[3..5];
print
        "now:              ", strftime("%Y-%m-%d", @t, $d, $m, $y), "\n",
        "60 days from now: ", strftime("%Y-%m-%d", @t, $d+60, $m, $y), "\n";

We can also see this penalty in the C version of the function:

#!/usr/bin/perl

use strict;
use warnings;
use Benchmark qw(:all);
use Inline "C";

print map { "$_\n" } production(), experimental(), ftime(), manual();
cmpthese(-1, {
   production      => \&production,
   experimental    => \&experimental,
   inline_strftime => \&ftime,
   inline_manual   => \&manual,
});

sub production {
   my $now_date_epoch = time();
   my $BDtarget = ($now_date_epoch - 5);
   my ($Bsec,$Bmin,$Bhour,$Bmday,$Bmon,$Byear,$Bwday,$Byday,$Bisdst)
= localtime($BDtarget);
   $Byear = ($Byear + 1900);
   $Bmon++;
   if ($Bmon < 10) {$Bmon = "0$Bmon";}
   if ($Bmday < 10) {$Bmday = "0$Bmday";}
   if ($Bhour < 10) {$Bhour = "0$Bhour";}
   if ($Bmin < 10) {$Bmin = "0$Bmin";}
   if ($Bsec < 10) {$Bsec = "0$Bsec";}
   my $BDtsSQLdate = "$Byear$Bmon$Bmday$Bhour$Bmin$Bsec";
}

sub experimental {
   my ($s, $min, $h, $d, $mon, $y) = localtime(time - 5);
   my $BDtsSQLdate = sprintf "%d%02d%02d%02d%02d%02d", $y + 1900,
     $mon + 1, $d, $h, $min, $s;
}

__DATA__
__C__

#include <time.h>
#include <stdio.h>

char* ftime() {
        static char ret[15];
        time_t now = time(NULL) - 5;
        strftime(ret, 14, "%Y%m%d%H%M%S", localtime(&now));
        return ret;
}

char* manual() {
        static char ret[15];
        time_t now   = time(NULL) - 5;
        struct tm *d = localtime(&now);

        snprintf(ret, 15, "%d%02d%02d%02d%02d%02d", d->tm_year + 1900,
                d->tm_mon + 1, d->tm_mday, d->tm_hour, d->tm_min, d->tm_sec);

        return ret;
}



-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to