Luke Bakken wrote:
>
> From: Larry Sandwick [mailto:[EMAIL PROTECTED]
> >
> > I have a scripted I wrote that uses *substr* on a file that has fixed
> > fields. I have to parse the file before I upload into MYSQL.
> >
> > Is there a more efficient way other than *substr*, I looked a
> > *pack* and
> > *unpack* . it did not make any sense to me.
>
> From my own limited performance testing (using the Benchmark module),
> I've found that substr is faster than unpack.

Thanks for that Luke. It seems you're right:

  use Benchmark;

  my $the_line = '       304 THE CASTLE TWO DRAWER CHEST  6-168  02/18/04';

  timethese(1E7, {
    'unpack' => sub {
      my ($item, $ldesc, $page, $d) = unpack 'A10xA28xA6xA8', $the_line;
    },
    'substr' => sub {
      my $item     = substr($the_line, 0, 10);
      my $ldesc    = substr($the_line, 11, 28);
      my $page     = substr($the_line, 40, 6);
      my $d        = substr($the_line, 47, 8);
    },
  });

**OUTPUT

  Benchmark: timing 10000000 iterations of substr, unpack...
    substr: 19 wallclock secs (18.19 usr +  0.00 sys = 18.19 CPU) @ 549752.61/s 
(n=10000000)
    unpack: 31 wallclock secs (30.64 usr +  0.00 sys = 30.64 CPU) @ 326370.76/s 
(n=10000000)

I think that will surpirise quite a few people.

Rob



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to