哪个更快,“find -exec”? 或“查找|” xargs -0'?

发布于 2024-07-23 18:37:52 字数 516 浏览 5 评论 0原文

在我的 Web 应用程序中,我使用 PHP 脚本渲染页面,然后从中生成静态 HTML 文件。 静态 HTML 提供给用户以提高性能。 HTML 文件最终会变得陈旧,需要删除。

我正在讨论两种编写驱逐脚本的方法。

第一种是使用单个 find 命令,如

find /var/www/cache -type f -mmin +10 -exec rm \{} \;

第二种形式是通过 xargs 进行管道传输,如

find /var/www/cache -type f -mmin +10 -print0 | xargs -0 rm

第一种形式为它找到的每个文件调用 rm ,而第二种形式只是将所有文件名发送到一个rm(但文件列表可能会很长)。

哪种形式会更快?

就我而言,缓存目录在几个 Web 服务器之间共享,因此这一切都是通过 NFS 完成的(如果这对这个问题很重要)。

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 HTML files become stale eventually, and need to be deleted.

I am debating between two ways to write the eviction script.

The first is using a single find command, like

find /var/www/cache -type f -mmin +10 -exec rm \{} \;

The second form is by piping through xargs, something like

find /var/www/cache -type f -mmin +10 -print0 | xargs -0 rm

The first form invokes rm for each file it finds, while the second form just sends all the file names to a single rm (but the file list might be very long).

Which form would be faster?

In my case, the cache directory is shared between a few web servers, so this is all done over NFS, if that matters for this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最舍不得你 2024-07-30 18:37:52

对于很多文件,xargs 版本比您发布的 -exec 版本要快得多,这是因为 rm 对于您想要删除的每个文件执行一次,而 xargs 会将尽可能多的文件集中到一个 rm 命令中。

对于数万或数十万个文件,时间的差别可能是一分钟或更短时间与一个小时的大部分时间之间的差异。

通过使用“+”而不是“\;”结束命令,您可以使用 -exec 获得相同的行为。 此选项仅在较新版本的 find 中可用。

以下两个大致相同:

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

请注意,xargs 版本在多处理器系统上的运行速度仍会稍快(几个百分点),因为某些工作可以并行化。 如果涉及大量计算,则尤其如此。

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.

我偏爱纯白色 2024-07-30 18:37:52

我预计 xargs 版本会稍微快一些,因为您不会为每个文件名生成一个进程。 但是,如果实践中确实存在很大差异,我会感到惊讶。 如果您担心 xargs 发送到每次 rm 调用的长列表,您可以将 -l 与 xargs 一起使用来限制它将使用的令牌数量。 但是,xargs 知道最长的命令行长度并且不会超出该长度。

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.

只想待在家 2024-07-30 18:37:52

find 命令内置了一个 -delete 选项,也许这也很有用?
http://lists.freebsd.org/pipermail/freebsd-questions /2004-7月/051768.html

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

戒ㄋ 2024-07-30 18:37:52

与 exec 和 find 相比,使用 xargs 更快。

我尝试使用 xargs 和 exec 计算 node_module 文件夹中带有 js 扩展名的文件中的行数。 所以输出如下。

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 的执行速度比 exec 快约 15 倍。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文