Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

In reply to (sacked: rename vs /bin/mv) Re: Re: mp3 organizer by sacked
in thread mp3 organizer by sacked

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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others exploiting the Monastery: (4)
    As of 2024-09-16 21:48 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.