gpt4 book ai didi

windows - 与 Windows dir 命令相比,为什么运行 opendir、readdir、stat 这么慢?

转载 作者:可可西里 更新时间:2023-11-01 09:23:45 25 4
gpt4 key购买 nike

我有一个使用 opendir 读取目录内容的 Perl 脚本:

opendir ( DIR, $path ) or next;
while (my $file = readdir DIR) {

然后我在做:

  • -s $file 获取每个文件的大小
  • (stat($file))[9]获取每个文件的修改时间

我在 Windows 机器上运行它并访问 Ubuntu 14.04 上的 Samba 共享。

一切正常,但与我在同一文件夹上运行 dir 列表时相比,该过程似乎运行得非常慢。

有谁知道为什么使用 opendir 比使用 dir 列表花费的时间长得多,如果有什么方法可以更改我的脚本以加快速度?

最佳答案

根据 perlport :

On Win32 stat() needs to open the file to determine the link count and update attributes that may have been changed through hard links. Setting ${^WIN32_SLOPPY_STAT} to a true value speeds up stat() by not performing this operation.

由于您正在访问的文件位于 Samba 共享中,因此打开它们可能相当耗时。此外,-s 会在后台调用 stat 系统调用,因此调用 -s 后跟 stat 是一种浪费.

以下应该更快:

local ${^WIN32_SLOPPY_STAT} = 1;

opendir my $dh, $path or die "Failed to opendir '$path': $!";

while (my $file = readdir $dh) {
my ($size, $mtime) = (stat $file)[7, 9];

say join "\t", $file, $size, $mtime;
}

关于windows - 与 Windows dir 命令相比,为什么运行 opendir、readdir、stat 这么慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37403143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com