我已经在1000 万个(空)文件上测试了此方法(以及所有其他方法),这些文件名为“ hello 00000001”到“ hello 10000000”(每个名称14个字节)。
更新: 我现在包括了在该方法上运行的四核'find |xargs'
(仍然没有“ sed”;只是echo> / dev / null)。
# Step 1. Build an array for 10 million files
# * RAM usage approx: 1.5 GiB
# * Elapsed Time: 2 min 29 sec
names=( hello\ * )
# Step 2. Process the array.
# * Elapsed Time: 7 min 43 sec
for (( ix=0, cnt=${#names[@]} ; ix<$cnt; ix++ )) ; do echo "${names[ix]}" >/dev/null ; done
这是总结针对上述测试数据运行时提供的答案的效果的摘要。这些结果仅涉及基本开销;即“ sed”未被调用。sed过程几乎肯定是最耗时的,但是我认为看看裸方法如何进行比较会很有趣。
丹尼斯'find |xargs'
使用单核的bash array
方法比no sed
运行方法花费* 4个小时21分钟** 。但是,“查找”提供的多核优势应该超过要求sed时显示的时差。正在处理文件...
| Time | RAM GiB | Per loop action(s). / The command line. / Notes
-----------+---------+---------+-----------------------------------------------------
Dennis | 271 min | 1.7 GiB | * echo FILENAME >/dev/null
Williamson cores: 1x2.66 MHz | $ time find -name 'hello *' -print0 | xargs -0 -I {} echo >/dev/null {}
| Note: I'm very surprised at how long this took to run the 10 million file gauntlet
| It started processing almost immediately (because of xargs I suppose),
| but it runs **significantly slower** than the only other working answer
| (again, probably because of xargs) , but if the multi-core feature works
| and I would think that it does, then it could make up the defecit in a 'sed' run.
| 76 min | 1.7 GiB | * echo FILENAME >/dev/null
cores: 4x2.66 MHz | $ time find -name 'hello *' -print0 | xargs -0 -I {} -P 0 echo >/dev/null {}
|
-----------+---------+---------+-----------------------------------------------------
fred.bear | 10m 12s | 1.5 GiB | * echo FILENAME >/dev/null
| $ time names=( hello\ * ) ; time for (( ix=0, cnt=${#names[@]} ; ix<$cnt; ix++ )) ; do echo "${names[ix]}" >/dev/null ; done
-----------+---------+---------+-----------------------------------------------------
l0b0 | ?@#!!# | 1.7 GiB | * echo FILENAME >/dev/null
| $ time while IFS= read -rd $'\0' path ; do echo "$path" >/dev/null ; done < <( find "$HOME/junkd" -type f -print0 )
| Note: It started processing filenames after 7 minutes.. at this point it
| started lots of disk thrashing. 'find' was using a lot of memory,
| but in its basic form, there was no obvious advantage...
| I pulled the plug after 20 minutes.. (my poor disk drive :(
-----------+---------+---------+-----------------------------------------------------
intuited | ?@#!!# | | * print line (to see when it actually starts processing, but it never got there!)
| $ ls -f hello * | xargs python -c '
| import fileinput
| for line in fileinput.input(inplace=True):
| print line '
| Note: It failed at 11 min and approx 0.9 Gib
| ERROR message: bash: /bin/ls: Argument list too long
-----------+---------+---------+-----------------------------------------------------
Reuben L. | ?@#!!# | | * One var assignment per file
| $ ls | while read file; do x="$file" ; done
| Note: It bombed out after 6min 44sec and approx 0.8 GiB
| ERROR message: ls: memory exhausted
-----------+---------+---------+-----------------------------------------------------
sed
为每个文件调用都会更快。我不确定是否有一种方法可以打开,编辑,保存和关闭;中的一系列文件sed
。如果速度至关重要,则可能需要使用其他程序,例如perl或python。