Hi all, The snippet of code below is taking some time in doing what it does (computes min, max and means of several arrays). Is there a way to speed up the process? Thanks, Stacy. foreach $checked (@avail) { chomp($checked); if ( $checked =~ /\.gz$/ ) { open(LOG, "/usr/local/gnu/bin/gunzip -c < $checked |") or die "$checked open failed: $!\n"; } else { open(LOG, "<$checked") or die "$checked open failed: $!\n"; } #reset data structures $MaxWSpd = $AvgWSpd = $WindDir = $OutTemp = 0; @maxwspd = (); @avgwspd = (); @outtemp = (); @winddir = (); while ($line = <LOG>) { %months = qw{JAN 1 FEB 2 MAR 3 APR 4 MAY 5 JUN 6 JUL 7 AUG 8 SEP 9 OCT 10 NOV 11 DEC 12}; next if $line =~ /^\D/; #Skip header next if $line =~ /9999/; #Skip bad records ($Date,$MaxWSpd,$AvgWSpd,$WindDir,$OutTemp) = (split(" ",$line)) +[0,2,3,4,8]; next if $MaxWSpd < 0; next if $AvgWSpd < 0; next if $WindDir < 0; next if $OutTemp < 0; ($day,$month,$year) = split('-',$Date); $month = sprintf "%02d", $months{$month}; if ($WindDir > 360) { $WindDir = $WindDir%360 } push (@maxwspd,$MaxWSpd); push (@avgwspd,$AvgWSpd); push (@outtemp,$OutTemp); push (@winddir,$WindDir); } #end of while $MeanAvg = sprintf("%4.1f", mean(\@avgwspd)); $MeanWDir = sprintf("%4.1f", mean(\@winddir)); $MaxWSpd = sprintf("%4.1f", max(\@maxwspd)); $MinWSpd = sprintf("%4.1f", min(\@maxwspd)); $MaxOutTemp = sprintf("%4.1f", max(\@outtemp)); $MinOutTemp = sprintf("%4.1f", min(\@outtemp)); push(@stats, "$year/$month/$day: $MinWSpd:$MaxWSpd $MeanAvg $MeanWDir $MinOutT +emp:$MaxOutTemp"); close(LOG); } #end of foreach foreach (@stats) { print $_, "\n"; } #-------------------- sub mean { my ($arrayref) = @_; my $result; foreach (@$arrayref) { $result += $_; } return $result / @$arrayref; } #-------------------- sub max { my ($arrayref) = @_; $max = -10; foreach (@$arrayref) { if ($_ > $max) { $max = $_ } } return $max; } #-------------------- sub min { my ($arrayref) = @_; $min = 50; foreach (@$arrayref) { if ($_ < $min) { $min = $_ } } return $min; } #--------------------

In reply to Inefficient code? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.