简体   繁体   English

如何在 linux 中指定用于脚本的 RAM 量

[英]how to specify the amount of RAM to use for a script in linux

Let's say I want to edit all the files in a folder, changing every header.假设我想编辑文件夹中的所有文件,更改每个 header。 I'm using this script:我正在使用这个脚本:

for thing in $(ls $1); $(ls $1) 中的东西; do sed -i '1c\SNP A2 A1 beta NP' $thing done做 sed -i '1c\SNP A2 A1 beta NP' $完成

The problem is that it takes a lot of time.问题是这需要很多时间。 So, I'd like to find a way to dedicate more RAM for this script, in order to do it quickly.所以,我想找到一种方法为这个脚本分配更多的内存,以便快速完成。 Is it possible?可能吗?

I'd like to find a way to dedicate more RAM for this script, in order to do it quickly.我想找到一种方法为这个脚本分配更多的内存,以便快速完成。 Is it possible?可能吗?

No. The tools you use, bash to enumerate your files and sed to edit them, take the RAM they need to do their work.不。您使用的工具 bash 用于枚举您的文件,sed 用于编辑它们,占用他们工作所需的 RAM。 They can't use more RAM even if they had a way to give it to them.即使他们有办法给他们更多的内存,他们也不能使用更多的内存。

You can run your sed operations in parallel.您可以并行运行 sed 操作。 This uses more cores on your machine and may finish faster.这会在您的机器上使用更多内核,并且可能会更快完成。 Put an & character after each command in the loop.在循环中的每个命令后放置一个&字符。 Something like this.像这样的东西。

for thing in $(ls $1); do 
  sed -i '1c\SNP A2 A1 beta N P' $thing &
done

With GNU Parallel you can control how many are run in parallel:使用 GNU Parallel,您可以控制并行运行的数量:

doit() {
  sed -i '1c\SNP A2 A1 beta N P' "$1"
}
export -f doit
ls $1 | parallel -j5 doit 

Adjust -j5 until you find the number that gives you most throughput.调整-j5直到找到能提供最大吞吐量的数字。

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

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