Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > increase performance

Reply
Thread Tools

increase performance

 
 
Rodrick Brown
Guest
Posts: n/a
 
      06-09-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

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


 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      06-09-2005
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
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      06-09-2005
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
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      06-09-2005
Mike Heins <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> On 2005-06-09, Rodrick Brown <(E-Mail Removed)> 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 <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
David K. Wall
Guest
Posts: n/a
 
      06-09-2005
A. Sinan Unur <(E-Mail Removed)> 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.
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      06-09-2005
"David K. Wall" <(E-Mail Removed)> wrote in
news:Xns967065E8CD199dkwwashere@216.168.3.30:

> A. Sinan Unur <(E-Mail Removed)> 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 <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      06-09-2005
John W. Krahn wrote:

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


Why did you write that instead of
my $date = 'date.txt';
?
-Joe
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      06-09-2005
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
 
Reply With Quote
 
Ilmari Karonen
Guest
Posts: n/a
 
      06-11-2005
Rodrick Brown <(E-Mail Removed)> 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.
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      06-14-2005
A. Sinan Unur <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Mike Heins <(E-Mail Removed)> wrote in
> news:(E-Mail Removed):


[...]

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Increase and show actual Modem Performance? Networking News Wireless Networking 17 03-21-2007 02:19 AM
How to Increase and show actual Modem Performance? Networking News MCSE 0 01-21-2007 09:36 AM
How to Increase and show actual Modem Performance? Networking News Microsoft Certification 0 01-21-2007 09:36 AM
Does RAID0 Really Increase Disk Performance? Silverstrand Front Page News 0 11-02-2006 02:09 AM
performance increase rpm v compile tony barratt Python 0 02-17-2005 09:05 AM



Advertisments