Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I would admit that this code does not belong to me. one of my senior wrote this code, but it takes more time to give the output, in our production server because of the volume.
#!/usr/bin/perl -w use strict; use File::Find (); my $dir = shift; die "Not a directory: '$dir'\n" unless -d $dir; print "Blame for $dir\n"; my %blame = (); my $total = 0; my (%uid, %username, %name); while (my ($shortname, $pw, $uid, undef, undef, undef, $name) = getpwe +nt) { next if exists $username{$uid}; $username{$uid} = $shortname; $name{$uid} = $name; } sub wanted { my (undef, undef, undef, undef, $uid, undef, undef, $size) = l +stat($File::Find::name); $blame{$uid} += $size; $total += $size; } sub by_usage { $blame{$b} <=> $blame{$a} } File::Find::find({wanted => \&wanted}, $dir); print " Usage Login Comment\n"; foreach my $uid (sort by_usage keys %blame) { next if $blame{$uid} == 0; printf "%13s %12s %s\n", $blame{$uid}, $username{$uid}||$uid, +$name{$uid}||$uid; } printf "%13s total disk used\n", $total;
Please let me know how can I minimize the code here to make the script run faster or give me some alternative unix commands.

Replies are listed 'Best First'.
Re: would like add your enhancement
by moritz (Cardinal) on May 09, 2008 at 10:53 UTC
    It looks like the script is mostly I/O bound, I don't know if there's anything you can do on the perl side to speed it up.

    If you want to try it nonetheless, you first have to identify the parts that are actually slow by profiling it.

    But maybe you just want to set quotas?