Which is faster, 'find -exec' or 'find | xargs -0'?

前端 未结 4 806
执念已碎
执念已碎 2021-01-05 10:14

In my web application I render pages using PHP script, and then generate static HTML files from them. The static HTML are served to the users to speed up performance. The HT

相关标签:
4条回答
  • 2021-01-05 10:58

    I expect the xargs version to be slightly faster as you aren't spawning a process for each filename. But, I would be surprised if there was actually much difference in practice. If you're worried about the long list xargs sends to each invocation of rm, you can use -l with xargs to limit the number of tokens it will use. However, xargs knows the longest cmdline length and won't go beyond that.

    0 讨论(0)
  • 2021-01-05 10:59

    The find command has a -delete option builtin in, perhaps that could be useful as well? http://lists.freebsd.org/pipermail/freebsd-questions/2004-July/051768.html

    0 讨论(0)
  • 2021-01-05 11:05

    Using xargs is faster as compared to exec with find.

    I tried to count no of lines in files in node_module folder with js extension using xargs and exec. So the output below.

    time find . -type f -name "*.js" -exec wc -l {} \;
    
    real    0m0.296s
    user    0m0.133s
    sys     0m0.038s
    
    time find . -type f -name "*.js" |xargs wc -l
    real    0m0.019s
    user    0m0.005s
    sys     0m0.006s
    

    xargs executes approx 15 times faster than exec.

    0 讨论(0)
  • 2021-01-05 11:16

    The xargs version is dramatically faster with a lot of files than the -exec version as you posted it, this is because rm is executed once for each file you want to remove, while xargs will lump as many files as possible together into a single rm command.

    With tens or hundreds of thousands of files, it can be the difference between a minute or less versus the better part of an hour.

    You can get the same behavior with -exec by finishing the command with a "+" instead of "\;". This option is only available in newer versions of find.

    The following two are roughly equivalent:

    find . -print0 | xargs -0 rm
    find . -exec rm \{} +
    

    Note that the xargs version will still run slightly faster (by a few percent) on a multi-processor system, because some of the work can be parallelized. This is particularly true if a lot of computation is involved.

    0 讨论(0)
提交回复
热议问题