Tom Allison wrote: > Gomez, Juan wrote: >> 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? >>
> I was going to say "use regex" but then I saw this nice example of > unpack which should be faster. > > So I tested it: > And ended up with this: > > [EMAIL PROTECTED]:~$ 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. If absolute speed is essential Juan should load up an array with a month number -> month name index and use substr to extract the month, day and year values out of the string, i.e. months[0] = 'Jan', months[1] = 'Feb'.... However, if his date string may change I'd use strftime() with substr(). use strict; use warnings; use POSIX qw/strftime/; 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); }, 'substr'=> sub { my @array = (substr($string, 0, 4), substr($string, 4, 2), substr($string, 6, 2)); }, }); print strftime('%b %d %Y', 0, 0, 0, substr($string, 6, 2), substr($string, 4, 2) - 1, substr($string, 0, 4) - 1900); Outputs: Benchmark: timing 1000000 iterations of regex, substr, unpack... regex: 12.1695 wallclock secs (11.59 usr + 0.00 sys = 11.59 CPU) @ 86303.62/s (n=1000000) substr: 5.70029 wallclock secs ( 5.54 usr + 0.00 sys = 5.54 CPU) @ 180603.21/s (n=1000000) unpack: 10.2787 wallclock secs (10.13 usr + 0.00 sys = 10.13 CPU) @ 98765.43/s (n=1000000) Aug 29 2005 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>