From: navarra@casbah.acns.nwu.edu (John Navarra)
Subject: Question: which is faster 'find -exec' or 'find | xargs' ??
Organization: Northwestern University, Evanston Illinois.
Date: Fri, 4 Sep 1992 04:10:33 GMT


I was experimenting with find and xargs. I issued the command
  find . -exec ls -ld {} \;
and the command
  find . -print | xargs ls -ld
and found they gave the same results. However, I noticed the execution
times for the two were MUCH different. If I time the two commands
from my home directory, which contains many files, here is what I get:

[casbah:47] ~ -> time find . -exec ls -ld {} \; > /dev/null
      287.4 real        39.1 user       219.9 sys
[casbah:48] ~ -> time find . -print | xargs ls -ld > /dev/null
       47.6 real         0.0 user        17.3 sys


Question: why is the xargs command MUCH faster?

While running the following commands, I ran ps -j many times. Here are some
of those results:

using exec:
PPID   PID  PGID   SID TT TPGID  STAT   UID  TIME COMMAND
 9119  9120  9120  9120 r6 21398   SOE   453  0:03 -bash (bash)
 9120 21383 21383  9120 r6 21398    SE   453  0:00 find . -exec ls -ld {} ;
 9120 21398 21398  9120 r6 21398    RE   453  0:00 ps -j
21383 21401 21383  9120 r6 21398    RE   453  0:00 ls -ld ./.elm/lsig.bak
 PPID   PID  PGID   SID TT TPGID  STAT   UID  TIME COMMAND
 9119  9120  9120  9120 r6 21409   SOE   453  0:03 -bash (bash)
 9120 21383 21383  9120 r6 21409    SE   453  0:00 find . -exec ls -ld {} ;
 9120 21409 21409  9120 r6 21409    RE   453  0:00 ps -j
21383 21412 21383  9120 r6 21409    RE   453  0:00 ls -ld ./bin/file_clean
 PPID   PID  PGID   SID TT TPGID  STAT   UID  TIME COMMAND
 9119  9120  9120  9120 r6 21422   SOE   453  0:03 -bash (bash)
 9120 21383 21383  9120 r6 21422    SE   453  0:00 find . -exec ls -ld {} ;
 9120 21422 21422  9120 r6 21422    RE   453  0:00 ps -j
21383 21426 21383  9120 r6 21422    RE   453  0:00 ls -ld ./bin/lastarg

In this case, bash is the parent to the find command, and find is the
parent to the ls -ld filename command. Thus, there is one find process,
plus X ls -ld filename processes where X is the number of files to be
listed. (obviously, I was not fast enough to capture all of them)
  

using xargs:
 PPID   PID  PGID   SID TT TPGID  STAT   UID  TIME COMMAND
 9119  9120  9120  9120 r6 21857   SOE   453  0:06 -bash (bash)
 9120 21817 21817  9120 r6 21857    SE   453  0:02 find . -print
 9120 21818 21817  9120 r6 21857    SE   453  0:03 xargs ls -ld
 9120 21857 21857  9120 r6 21857    RE   453  0:00 ps -j
 PPID   PID  PGID   SID TT TPGID  STAT   UID  TIME COMMAND
 9119  9120  9120  9120 r6 21867   SOE   453  0:06 -bash (bash)
 9120 21817 21817  9120 r6 21867    SE   453  0:02 find . -print
 9120 21818 21817  9120 r6 21867    SE   453  0:03 xargs ls -ld
 9120 21867 21867  9120 r6 21867    RE   453  0:00 ps -j
21818 21868 21817  9120 r6 21867    RE   453  0:00 ls -ld ./awk/shellreport.naw
 PPID   PID  PGID   SID TT TPGID  STAT   UID  TIME COMMAND
 9119  9120  9120  9120 r6 21900   SOE   453  0:07 -bash (bash)
 9120 21817 21817  9120 r6 21900    RE   453  0:04 find . -print
 9120 21818 21817  9120 r6 21900    RE   453  0:07 xargs ls -ld
 9120 21900 21900  9120 r6 21900    RE   453  0:00 ps -j
21818 21908 21817  9120 r6 21905    RE   453  0:00 ls -ld ./perl/perl_info/docs

In this case, bash is the parent of both the find and xargs commands. And
xargs is the parent of the ls -ld filename commands. Thus, there is
one find process, one xargs process, and X ls -ld processes (again, where
X is the number of files to be listed.)

The way I count it, the xargs command has one extra process, the overhead
of setting up a pipe, and the fact that two commands are being used vs
one. But, for some reason the xargs command was MUCH faster than the
find -exec command.  

What's the deal? From what I can gather, find -exec will write the
stdout X times in this case where xargs writes in chunks. Is that
correct?
 

-tms 
-- 
You can get further with a kind word | You can get further with a kind word
and a gun than a kind word alone.    | and a phaser than a kind word and a gun.
          --al capone                |           -- John Navarra
=======From the Lab of the MaD ScIenTIst....navarra@casbah.acns.nwu.edu========