increase performance

R

Rodrick Brown

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";
}
}
}
 
J

John W. Krahn

Rodrick said:
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
 
J

John W. Krahn

Rodrick said:
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
 
A

A. Sinan Unur

....

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
 
D

David K. Wall

A. Sinan Unur said:
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.
 
A

A. Sinan Unur

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
 
J

John W. Krahn

Joe said:
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
 
I

Ilmari Karonen

Rodrick Brown said:
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
 
A

Anno Siegel

A. Sinan Unur said:
[...]

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
 
A

Anno Siegel

John W. Krahn said:
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
 
A

Anno Siegel

John W. Krahn said:
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
 
J

John W. Krahn

Anno said:
John W. Krahn said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top