Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: mp3 organizer

by salvadors (Pilgrim)
on Jan 31, 2001 at 20:04 UTC ( [id://55477]=note: print w/replies, xml ) Need Help??


in reply to mp3 organizer

Quite nice. But rather than making system calls to /bin/mv, you should really be using the builtin rename command...

Tony

Replies are listed 'Best First'.
(sacked: rename vs /bin/mv) Re: Re: mp3 organizer
by sacked (Hermit) on Feb 01, 2001 at 13:03 UTC
    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://55477]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-09-16 23:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (22 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.