Thanks for the suggestion! I was curious as to whether rename would be faster, though, since it requires one call per mp3, whereas /bin/mv requires only one call per directory. After a quick scan of my collection, I made a conservative estimate of five mp3s per directory (meaning, of course, five times as many calls are required for rename as /bin/mv). The results of a benchmark, however, were enlightening. Here is the benchmarking code I used:
#!/usr/bin/perl -w
#
# system('/bin/mv') vs rename
use strict;
use Benchmark;
use vars qw($rename_cnt $system_cnt $mp3dir1 $mp3dir2 @files @f1 @f2 $
+i $flip);
# $flip switches once per @files runs of the rename sub,
# then switches every call to the /bin/mv sub
$flip = $i = 0;
# flip-flop between these two dirs for move operations
$mp3dir1 = '/opt/mp3/';
$mp3dir2 = '/opt/tmp/';
# get file list
opendir(DIR, $mp3dir2) or die "can't open $mp3dir2 for read: $!\n\n";
@files = grep { /\.[Mm][Pp]3$/ } readdir(DIR);
closedir(DIR) or warn "error closing $mp3dir2: $!\n\n";
@f1 = map { $mp3dir1 . $_ } @files;
@f2 = map { $mp3dir2 . $_ } @files;
# run the func a few times
$rename_cnt = $#files * 10;
$system_cnt = int($rename_cnt / 5);
my $t = timeit($rename_cnt, \&rename_move_files);
print "$rename_cnt loops of 'rename' took:",timestr($t),"\n";
$t = timeit($system_cnt, \&system_move_files);
print "$system_cnt loops of 'system' took:",timestr($t),"\n";
exit;
sub system_move_files {
my ($src,$dst) = ($flip % 2) ? (\@f1,$mp3dir2) : (\@f2,$mp3dir1);
system('/bin/mv', @$src, qq{$dst}) == 0
or die "system('/bin/mv'): $?\n\n";
++$flip;
}
sub rename_move_files {
my ($src,$dst) = ($flip % 2) ? (\@f1,\@f2) : (\@f2,\@f1);
# so we can run this more than @files times
my $j = $i % $#files;
rename qq{$src->[$j]}, qq{$dst->[$j]} or die "can't rename $src->[$j
+]: $!\n\n";
++$i;
++$flip if ($i % $#files == 0);
}
__END__
results ranged from:
1300 loops of 'rename' took: 0 wallclock secs ( 0.01 usr + 0.05 sys =
+ 0.06 CPU) @ 21666.67/s (n=1300)
260 loops of 'system' took: 2 wallclock secs ( 0.08 usr 0.13 sys + 0
+.44 cusr 1.46 csys = 2.11 CPU) @ 1238.10/s (n=260)
to:
1300 loops of 'rename' took: 0 wallclock secs ( 0.02 usr + 0.04 sys =
+ 0.06 CPU) @ 21666.67/s (n=1300)
260 loops of 'system' took: 2 wallclock secs ( 0.03 usr 0.04 sys + 0
+.38 cusr 1.67 csys = 2.12 CPU) @ 3714.29/s (n=260)
Looks like the rename function wins.
--
"All we gotta do is apply the final touches!" -Parappa
sacked | [reply] [d/l] |