home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / unix / question / 11036 < prev    next >
Encoding:
Internet Message Format  |  1992-09-14  |  2.8 KB

  1. Path: sparky!uunet!mcsun!sun4nl!orcenl!nl.oracle.com!rlyle
  2. From: rlyle@nl.oracle.com (The Wizard of Ozje - Garf)
  3. Newsgroups: comp.unix.questions
  4. Subject: Re: Question: which is faster 'find -exec' or 'find | xargs' ??
  5. Message-ID: <2817@nlsun1.oracle.nl>
  6. Date: 14 Sep 92 12:48:11 GMT
  7. References: <1992Sep4.041033.23158@news.acns.nwu.edu>
  8. Sender: news@nl.oracle.com
  9. Organization: Oracle Europe
  10. Lines: 62
  11. Nntp-Posting-Host: nlsu28
  12.  
  13. In article <1992Sep4.041033.23158@news.acns.nwu.edu>, navarra@casbah.acns.nwu.edu (John Navarra) writes:
  14. |> 
  15. |> I was experimenting with find and xargs. I issued the command
  16. |>   find . -exec ls -ld {} \;
  17. |> and the command
  18. |>   find . -print | xargs ls -ld
  19. |> and found they gave the same results. However, I noticed the execution
  20. |> times for the two were MUCH different. If I time the two commands
  21. |> from my home directory, which contains many files, here is what I get:
  22. |> 
  23. |> [casbah:47] ~ -> time find . -exec ls -ld {} \; > /dev/null
  24. |>       287.4 real        39.1 user       219.9 sys
  25. |> [casbah:48] ~ -> time find . -print | xargs ls -ld > /dev/null
  26. |>        47.6 real         0.0 user        17.3 sys
  27. |> 
  28. |> 
  29. |> Question: why is the xargs command MUCH faster?
  30.  
  31. ... [Lot's of stuff deleted]
  32.  
  33. Point 1:
  34.  
  35. The time command for the second case (with xargs) is only timing the find command, ignoring the time spent doing the xargs ls command.
  36.  
  37. If you want to time the pipeline try timing a parent shell which executes the pipeline.  There will ofcourse be some overhead concerning the startup of the shell, but that is constant and therefore tends to zero importance as the size of the job being done by the shell script increases.
  38.  
  39. #!/bin/sh
  40. find . -print | xargs ls -ld > /dev/null
  41.  
  42. Also, if you run the above script a few times, things will speed up due to the OS buffering mem, file descriptors et al.  4 or 5 goes should be enough to get a steady result on an evenly loaded machine.
  43.  
  44. Point 2:
  45.  
  46. I'm not so sure about this (haven't seen the source for "find" but ...) If the
  47. -exec primitive is to be believed then the find process must fork and exec an ls
  48. for every line of output from the find.  That's an awful lot of work!  Which
  49. version of fork you're using and on which machine will make a difference also,
  50. though not as dramatically as the difference between using a pipeline, and doing
  51. for/exec for everyline of output.
  52.  
  53. Share and enjoy ...
  54.  
  55.  
  56. --Rob.
  57.  
  58. |> 
  59. |> What's the deal? From what I can gather, find -exec will write the
  60. |> stdout X times in this case where xargs writes in chunks. Is that
  61. |> correct?
  62. |>  
  63.  
  64.  
  65.  
  66. -- 
  67. Robert P K Lyle        #include <std_disclaimer.h>    **********************
  68. UNIX System Manager                     If I could play the
  69. ORACLE Nederland                    Blue Peter theme tune
  70. Rijnzathe 6,                            here, I would.
  71. 3454 PV                          **********************
  72. De Meern
  73. E-mail:  rlyle@nl.oracle.com                FAX:     +31 3406 65603
  74. Voice:   +31 3406 94211                    Private: +31 10 4809140
  75.