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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |