I need a little assistance with trying to rewrite code....i am new to perl pieced this together but i need this to go a lot faster. for 258 records it took 67 minutes. I need to do at least a few million directory look ups using the code below. but this will take forever to run at the current speed. i got it to read a file, lookup mailbox and write to file after completed but the speed and maybe structure maybe be wrong Can anyone help with conversion or pointers......still learning.........

#!/usr/local/bin/perl use strict; use warnings; use File::Copy; use File::Slurp qw(read_dir); use File::Find; use Data::Dumper; # Dry run. 0 will execute use constant DRY=>qq(1); # Hardcoding the file path as of now use constant FILE=>qq(test.txt); # Successful operation - stats file use constant STATS_FILE => qq(mbox_stats.txt); # Failed mailbox operations use constant MBOX_UNKNOWN => qq(mbox_unknown.txt); #TOP Level volume directory use constant VOL=>qq(/home/folder/); use constant DEBUG => 1; #Total space saving if removed my $tsavings = 0; #Assuming \n as the eod of line local $/ = "\n"; # Read the volume directory and load all the subdirectories under it opendir (DIR, VOL) or die "Unable to open VOLUME: $!"; my @dh = grep { !/^\.{1,2}$/ } readdir (DIR); closedir (DIR); # Open the file containing the id and mbox for reading open my $file, '<', FILE; # Open the file to log unknown mailboxes open MB_UNK, ">", MBOX_UNKNOWN or die $!; #Open the file to log stats open STATS, ">", STATS_FILE or die $!; # Loop through each lines of the file. We need to read each directory. while(<$file>) { chomp;# Strip the line break character at the end of the line # Strip the mbox and id, assuming the file contains mbox:id my($mbox,$id)=split(',',$_); if (!$mbox){ print "No mbox for $id\n"; next; } if (!$id) { print "No id\n"; next; } print ("Processing mailbox search for id $id \n") if DEBUG; #Parse the mbox hash to retrieve the two directory names my ($u,$v,$mbox_path); if ( $mbox =~ /\d*(\d{2})(\d{2})$/) { $u = reverse $1; $v = reverse $2; } # Build the volume path. So that now we have a pach something like t +his # VOL/v*/$mb - we still have to determine v* by reading the direct +ory of VOL my $mb = qq($v/$u/$mbox); my @dir = map { VOL . '/' . $_ ."/$mb" } @dh; foreach(@dir) { if (-d $_) { $mbox_path = $_; my $dirsize = 0;# Setting size to 0 each loop find(sub { $dirsize += -f $_ ? -s _ : 0}, "$mbox_path"); $tsavings +=$dirsize;# Adding dirsize to total # print "Path to mail directory $mbox_path . Directory size + = $dirsize bytes\n"; # (DRY?print "Doing dry run only\n":movemailbox($mbox_path) +); print STATS "$mbox:$id:$mbox_path:$dirsize\n"; } } if (!$mbox_path) { # print "No Mailbox found for $id \n"; print MB_UNK "$mbox:$id\n"; } } close (MB_UNK); close (STATS); close ($file); print "Total size of directory to be freed $tsavings\n"; sub movemailbox { my $m = shift; move($m, $m."-trash" ) or die "move $m failed: $!"; return; } __END__

In reply to Increase script speed by ctrevgo_learn_perl

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.