in reply to Myth busted: Shell isn't always faster than Perl

The problem with the shell version is that it's spawning a new process for every rm. The usual practice is to use xargs in conjunction with find.
time find . -type f -print | xargs rm
I don't know how much that will affect your timings, though.

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: Myth busted: Shell isn't always faster than Perl
by jdhedden (Deacon) on Dec 30, 2005 at 20:36 UTC
    I'd say it's a wash. Trying it several times, the best times I got were:
    time perl -MFile::Find -e'finddepth sub { unlink if -f }, @ARGV' /tmp
    real    0m3.111s
    user    0m0.821s
    sys     0m2.233s
    
    time find /tmp -type f | xargs rm
    real    0m3.312s
    user    0m0.760s
    sys     0m2.511s
    
    And the varied widely - anywhere up to 5 seconds.

    Remember: There's always one more bug.
      I tested my original script with the
      return if -d; unlink $_
      against the golfed
      unlink $_ if -f;
      and the golfed -f test seems to be a bit slower. Maybe because unlink somehow gets called for each directory then stopped? Whereas the 'return if -d ' returns immediately. BUT the improved shell with null
      find . -type f -print0 | xargs -0 rm
      seems to win :-(
      time -d-test Gtk3 real 0m0.412s user 0m0.074s sys 0m0.337s time -f-test Gtk3 real 0m0.478s user 0m0.076s sys 0m0.388s time find . -type f -print0 | xargs -0 rm real 0m0.334s user 0m0.012s sys 0m0.321s

      I'm not really a human, but I play one on earth. flash japh
Re^2: Myth busted: Shell isn't always faster than Perl
by zentara (Cardinal) on Dec 30, 2005 at 20:16 UTC
    Yeah, that brings the shell closer in speed, BUT it starts complaining AND skipping filenames with spaces in them, I believe that is why the original contruct was the way it was.

    I'm not really a human, but I play one on earth. flash japh
      it starts complaining AND skipping filenames with spaces in them

      ...And that is precisely why find has the -print0 switch and xargs has the -0 (or --null) switch.

      find . -type f -print0 | xargs -0 rm
      We're building the house of the future together.
        This leaves the problem of a very big list, in which case the shell (I presume) would refuse to start the xargs side.

        Flavio
        perl -ple'$_=reverse' <<<ti.xittelop@oivalf

        Don't fool yourself.