简体   繁体   English

虽然读线很慢

[英]While read line is very slow

I want to create thumbs for large list of images. 我想为大量图像创建缩略图。 The problem is, it appears that while read line is very slow with large lists. 问题是,似乎在大列表读取行中速度很慢。 one solution that i can think of is to create files that contain max 500 lines and then read them one by one. 我能想到的一种解决方案是创建最多包含500行的文件,然后一一读取。 but is there any smart solution for this problem? 但是有没有针对此问题的智能解决方案?

while read line; do
  if [ -e "$line" ] && [ ! -z "$line" ]; then
              ...
  fi
}
done <<< "$imagesList"

Your problem is that you're using a HERE string to read in these lines via <<< . 您的问题是您正在使用HERE字符串通过<<<读取这些行。 This will be slow if the HERE string is very large. 如果HERE字符串很大,这将很慢。

If $imgageList is a file, you can do a file redirect and this will be a lot faster: 如果$imgageList是文件,则可以执行文件重定向,这会快很多:

while read line
do
    if [ -e "$line" -a ! -z "$line" ]
    then
       ...
    fi
done < "$imagesList_file"  # Redirect from a file.

You might be able to do this: 您可能可以执行以下操作:

echo "$imagesList" | while read line
    do
    if [ -e "$line" -a ! -z "$line" ]
    then
       ...
    fi
done

But, I would be worried about overloading the command line. 但是,我会担心命令行超载。 In Linux/Unix systems, this is defined in the /usr/include/sys/syslimits.h or /usr/include/syslimits.h . 在Linux / Unix系统中,这是在/usr/include/sys/syslimits.h/usr/include/syslimits.h定义的。 It's 1024 * 256 on my system or 262,144 bytes. 在我的系统上为1024 * 256或262,144字节。 This sounds like a lot, but can be deceptive. 这听起来很多,但可能具有欺骗性。 File names can be quite long -- especially if you include the directory path in them. 文件名可能会很长-特别是如果其中包含目录路径。 This is usually long enough to pass when you're testing, but fail when you really, really are depending upon it to work. 当您进行测试时,这通常足够长,但是如果您真的真的要依靠它来工作,则失败了。 And. 和。 it fails silently. 它无声地失败了。 You never know that the last few file names were dropped off. 您永远不会知道最后几个文件名已删除。

Your best bet is to create a file with the list of image names instead of loading them up in an environment variable. 最好的选择是创建一个包含图像名称列表的文件,而不是将其加载到环境变量中。

Let's try again: 让我们再试一次:

echo $imagelist | xargs --delimiter=\  --max-args=1 -i echo "Do something with {}"

--- 8< First version --- 8< --- -8 <第一版-8 <-

Try something like this: 尝试这样的事情:

for IMG in $imageList; do
    echo "Do something with ${IMG}"
done

--- >8 First version --- >8 --- -> 8第一版-> 8-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM