increase performance

Discussion in 'Perl Misc' started by Rodrick Brown, Jun 9, 2005.

  1. Hello all I have a script that processes the following data I could possibly
    speed it up

    0107
    0205
    0304
    0405
    0105
    0805

    the script just converts the output to

    Jan07
    feb05
    mar03
    apr05

    etc...

    Here is a sample of how i'm doing this


    #!/usr/bin/perl

    use warnings;

    my %months = ( 1=>"jan", 2=>"feb", 3=>"mar", 4=>"apr", 5=>"may", 6=>"june",
    7=>"jul", 8=>"aug", 9=>"sep", 10=>"oct", 11=>"nov",
    12=>"dec" );

    my $date = "./date.txt";

    open LOG, $date or die("unable to open file: $!\n");

    while(<LOG>)
    {
    foreach my $m (keys(%months))
    {
    if( $m eq substr($_,1,1))
    {
    my $days = substr($_,2,2);
    print "$months{$m}$days\n";
    }
    }
    }

    --
    Rodrick R. Brown
    rodrick.brown[@]gmail.com
     
    Rodrick Brown, Jun 9, 2005
    #1
    1. Advertising

  2. Rodrick Brown wrote:
    > Hello all I have a script that processes the following data I could possibly
    > speed it up
    >
    > 0107
    > 0205
    > 0304
    > 0405
    > 0105
    > 0805
    >
    > the script just converts the output to
    >
    > Jan07
    > feb05
    > mar03
    > apr05
    >
    > etc...
    >
    > Here is a sample of how i'm doing this
    >
    >
    > #!/usr/bin/perl
    >
    > use warnings;
    >
    > my %months = ( 1=>"jan", 2=>"feb", 3=>"mar", 4=>"apr", 5=>"may", 6=>"june",
    > 7=>"jul", 8=>"aug", 9=>"sep", 10=>"oct", 11=>"nov",
    > 12=>"dec" );
    >
    > my $date = "./date.txt";
    >
    > open LOG, $date or die("unable to open file: $!\n");
    >
    > while(<LOG>)
    > {
    > foreach my $m (keys(%months))
    > {
    > if( $m eq substr($_,1,1))
    > {
    > my $days = substr($_,2,2);
    > print "$months{$m}$days\n";
    > }
    > }
    > }
    >



    my %months = qw( 01 jan 02 feb 03 mar 04 apr 05 may 06 june
    07 jul 08 aug 09 sep 10 oct 11 nov 12 dec );

    my $date = './date.txt';

    open LOG, $date or die "unable to open file: $!\n";

    while ( <LOG> ) {
    my $mon = substr $_, 0, 2;
    substr $_, 0, 2, $months{ $mon } || $mon;
    }

    __END__



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Jun 9, 2005
    #2
    1. Advertising

  3. Rodrick Brown wrote:
    > Hello all I have a script that processes the following data I could possibly
    > speed it up
    >
    > 0107
    > 0205
    > 0304
    > 0405
    > 0105
    > 0805
    >
    > the script just converts the output to
    >
    > Jan07
    > feb05
    > mar03
    > apr05
    >
    > etc...
    >
    > Here is a sample of how i'm doing this
    >
    >
    > #!/usr/bin/perl
    >
    > use warnings;
    >
    > my %months = ( 1=>"jan", 2=>"feb", 3=>"mar", 4=>"apr", 5=>"may", 6=>"june",
    > 7=>"jul", 8=>"aug", 9=>"sep", 10=>"oct", 11=>"nov",
    > 12=>"dec" );
    >
    > my $date = "./date.txt";
    >
    > open LOG, $date or die("unable to open file: $!\n");
    >
    > while(<LOG>)
    > {
    > foreach my $m (keys(%months))
    > {
    > if( $m eq substr($_,1,1))
    > {
    > my $days = substr($_,2,2);
    > print "$months{$m}$days\n";
    > }
    > }
    > }
    >


    my %months = qw( 01 jan 02 feb 03 mar 04 apr 05 may 06 june
    07 jul 08 aug 09 sep 10 oct 11 nov 12 dec );

    my $date = './date.txt';

    open LOG, $date or die "unable to open file: $!\n";

    while ( <LOG> ) {
    my $mon = substr $_, 0, 2;
    substr $_, 0, 2, $months{ $mon } || $mon;
    print;
    }

    __END__



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Jun 9, 2005
    #3
  4. Mike Heins <> wrote in
    news::

    > On 2005-06-09, Rodrick Brown <> wrote:
    >> Hello all I have a script that processes the following data I could
    >> possibly speed it up
    >>
    >> 0107
    >> 0205
    >> 0304
    >> 0405
    >> 0105
    >> 0805
    >>
    >> the script just converts the output to
    >>
    >> Jan07
    >> feb05
    >> mar03
    >> apr05
    >>
    >> etc...
    >>
    >> Here is a sample of how i'm doing this


    ....

    > There's no point in using a hash for this type of thing if you
    > don't do a hash key lookup.


    Agreed.

    However, the easiest way to speed this task up by an order of magnitude
    is to avoid printing. As (I think) Anno says: Print rarely, print late.

    But to decide how rarely, and how late, one would have to know more.

    As a simple experiment, take the following script:

    #! /usr/bin/perl
    use strict;
    use warnings;

    my @months = qw(invalid
    jan feb mar apr may jun
    jul aug sep oct nov dec
    );

    while(<DATA>) {
    next unless /^(\d\d)(\d\d)$/;
    print "$months[0 + $1]$2\n";
    }

    __END__

    In the version I will use illustrate, I have 10,000 lines of data
    following __END__.

    I am on Windows XP Pro, perl v.5.8.6.811 (ActiveState), Acer AMD64
    Laptop with 1 GB RAM:

    TimeThis : Command Line : perl ttt.pl
    TimeThis : Start Time : Wed Jun 08 23:46:14 2005
    TimeThis : End Time : Wed Jun 08 23:46:16 2005
    TimeThis : Elapsed Time : 00:00:01.578

    Now, replace the script with the following:

    #! /usr/bin/perl
    use strict;
    use warnings;

    my @months = qw(invalid
    jan feb mar apr may jun
    jul aug sep oct nov dec
    );

    my $result;

    while(<DATA>) {
    next unless /^(\d\d)(\d\d)$/;
    $result .= "$months[0 + $1]$2\n";
    }

    __END__

    On the exact same data set, we get:

    TimeThis : Command Line : perl ttt.pl
    TimeThis : Start Time : Thu Jun 09 00:02:31 2005
    TimeThis : End Time : Thu Jun 09 00:02:31 2005
    TimeThis : Elapsed Time : 00:00:00.187

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Jun 9, 2005
    #4
  5. A. Sinan Unur <> wrote:

    > However, the easiest way to speed this task up by an order of
    > magnitude is to avoid printing. As (I think) Anno says: Print
    > rarely, print late.


    Not that it matters, but I think it's Uri Guttman who says that.

    Anyone know what's up with Uri? I haven't seen any posts from him here
    for at least a month or two.
     
    David K. Wall, Jun 9, 2005
    #5
  6. "David K. Wall" <> wrote in
    news:Xns967065E8CD199dkwwashere@216.168.3.30:

    > A. Sinan Unur <> wrote:
    >
    >> However, the easiest way to speed this task up by an order of
    >> magnitude is to avoid printing. As (I think) Anno says: Print
    >> rarely, print late.

    >
    > Not that it matters, but I think it's Uri Guttman who says that.


    You are right. I should have just searched Google for the phrase. Thank
    you for the correction.

    > Anyone know what's up with Uri? I haven't seen any posts from him
    > here for at least a month or two.


    I had hoped he was enjoying a well deserved vacation, but he seems to be
    active elsewhere.

    Sinan

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Jun 9, 2005
    #6
  7. Rodrick Brown

    Joe Smith Guest

    John W. Krahn wrote:

    > my $date = './date.txt';


    Why did you write that instead of
    my $date = 'date.txt';
    ?
    -Joe
     
    Joe Smith, Jun 9, 2005
    #7
  8. Joe Smith wrote:
    > John W. Krahn wrote:
    >
    >> my $date = './date.txt';

    >
    > Why did you write that instead of
    > my $date = 'date.txt';
    > ?


    That is the way the OP wrote it and they both refer to the same file so I
    didn't change it.


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Jun 9, 2005
    #8
  9. Rodrick Brown <> kirjoitti 09.06.2005:
    > Hello all I have a script that processes the following data I could possibly
    > speed it up
    >
    > 0107
    > 0205
    > 0304
    > 0405
    > 0105
    > 0805
    >
    > the script just converts the output to
    >
    > Jan07
    > feb05
    > mar03
    > apr05


    I'd do that with a one-liner:

    perl -pe 's/\d\d/(qw(jan feb mar apr may jun jul aug sep oct nov dec))[$&-1]/e' date.txt

    --
    Ilmari Karonen
    To reply by e-mail, please replace ".invalid" with ".net" in address.
     
    Ilmari Karonen, Jun 11, 2005
    #9
  10. Rodrick Brown

    Anno Siegel Guest

    A. Sinan Unur <> wrote in comp.lang.perl.misc:
    > Mike Heins <> wrote in
    > news::


    [...]

    > However, the easiest way to speed this task up by an order of magnitude
    > is to avoid printing. As (I think) Anno says: Print rarely, print late.


    Uri. I agree with him.

    Anno
     
    Anno Siegel, Jun 14, 2005
    #10
  11. Rodrick Brown

    Anno Siegel Guest

    John W. Krahn <> wrote in comp.lang.perl.misc:
    > Rodrick Brown wrote:


    [...]

    > my %months = qw( 01 jan 02 feb 03 mar 04 apr 05 may 06 june
    > 07 jul 08 aug 09 sep 10 oct 11 nov 12 dec );


    We have seen various ways of setting up this hash in this thread. I'd
    go out of my way to let the computer do the counting:

    my %months;
    @months{ 1 .. 12} = qw(jan feb mar apr may jun jul aug sep oct nov dec);

    I think only Ilmari's one-liner didn't explicitly number all months.

    Anno
     
    Anno Siegel, Jun 14, 2005
    #11
  12. Rodrick Brown

    Anno Siegel Guest

    John W. Krahn <> wrote in comp.lang.perl.misc:
    > Rodrick Brown wrote:


    [...]

    > my %months = qw( 01 jan 02 feb 03 mar 04 apr 05 may 06 june
    > 07 jul 08 aug 09 sep 10 oct 11 nov 12 dec );


    We have seen various ways of setting up this hash in this thread. I'd
    go out of my way to let the computer do the counting:

    my %months;
    @months{ 1 .. 12} = qw(jan feb mar apr may jun jul aug sep oct nov dec);

    Anno
     
    Anno Siegel, Jun 14, 2005
    #12
  13. Anno Siegel wrote:
    > John W. Krahn <> wrote in comp.lang.perl.misc:
    >
    >>Rodrick Brown wrote:

    >
    >
    > [...]
    >
    >
    >>my %months = qw( 01 jan 02 feb 03 mar 04 apr 05 may 06 june
    >> 07 jul 08 aug 09 sep 10 oct 11 nov 12 dec );

    >
    >
    > We have seen various ways of setting up this hash in this thread. I'd
    > go out of my way to let the computer do the counting:
    >
    > my %months;
    > @months{ 1 .. 12} = qw(jan feb mar apr may jun jul aug sep oct nov dec);


    And with the leading zeros:

    @months{ '01' .. '12' } = qw(jan feb mar apr may jun jul aug sep oct nov dec);


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Jun 14, 2005
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. anupam
    Replies:
    4
    Views:
    880
    Allan Herriman
    Sep 3, 2004
  2. Cam Acosta

    increase response buffer size

    Cam Acosta, Nov 10, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    9,937
    Cam Acosta
    Nov 10, 2003
  3. Bob
    Replies:
    1
    Views:
    627
    .NET Follower
    Feb 13, 2004
  4. Benny
    Replies:
    1
    Views:
    455
    Paul Wistrand
    Mar 1, 2004
  5. tony barratt

    performance increase rpm v compile

    tony barratt, Feb 17, 2005, in forum: Python
    Replies:
    0
    Views:
    321
    tony barratt
    Feb 17, 2005
Loading...

Share This Page