Re: Date in perl



Gomez, Juan wrote:
Hi all
I have a problem need to work with date
I have a input like these : 20050829 and I need to change it to something like this : Aug 29 2005
but it still eludes me how to do that
can anyone help me please?
thanks
Armando

I was going to say "use regex" but then I saw this nice example of unpack which should be faster.


So I tested it:


#!/usr/bin/perl #

use strict;
use warnings;
use Benchmark ':hireswallclock';

my $string = "20050829";

timethese(1000000, {
    'regex' => sub { my @array = $string=~/(\d\d\d\d)(\d\d)(\d\d)/;},
    'unpack'=> sub { my @array = unpack("A4A2A2",$string); },
});


And ended up with this:

tallison@isengard:~$ benchmark.pl
Benchmark: timing 1000000 iterations of regex, unpack...
regex: 2.75194 wallclock secs ( 2.75 usr + 0.00 sys = 2.75 CPU) @ 363636.36/s (n=1000000)
unpack: 2.15273 wallclock secs ( 2.15 usr + 0.00 sys = 2.15 CPU) @ 465116.28/s (n=1000000)


I think the POSIX modules would be considerably slower but I did not test them.
.