[英]Removing forks in a shell script so that it runs well in Cygwin
我正在尝试在Cygwin的Windows上运行shell脚本。 我遇到的问题是它在下面的代码段中运行得非常慢。 从一点谷歌搜索,我相信它是由于脚本中有大量的fork()调用,并且由于Windows必须使用Cygwins仿真,它只是慢慢爬行。
一个典型的场景是在Linux中,脚本将在<10秒内完成(取决于文件大小),但在Windows上的Cygin中,对于相同的文件,它将花费将近10分钟.....
所以问题是,如何删除其中一些分支仍然让脚本返回相同的输出。 我并不期待奇迹,但我想将这10分钟的等待时间缩短一点。
谢谢。
check_for_customization(){
filename="$1"
extended_class_file="$2"
grep "extends" "$filename" | grep "class" | grep -v -e '^\s*<!--' | while read line; do
classname="$(echo $line | perl -pe 's{^.*class\s*([^\s]+).*}{$1}')"
extended_classname="$(echo $line | perl -pe 's{^.*extends\s*([^\s]+).*}{$1}')"
case "$classname" in
*"$extended_classname"*) echo "$filename"; echo "$extended_classname |$classname | $filename" >> "$extended_class_file";;
esac
done
}
更新 :更改了正则表达式并使用了更多perl:
check_for_customization(){
filename="$1"
extended_class_file="$2"
grep "^\(class\|\(.*\s\)*class\)\s.*\sextends\s\S*\(.*$\)" "$filename" | grep -v -e '^\s*<!--' | perl -pe 's{^.*class\s*([^\s]+).*extends\s*([^\s]+).*}{$1 $2}' | while read classname extended_classname; do
case "$classname" in
*"$extended_classname"*) echo "$filename"; echo "$extended_classname | $classname | $filename" >> "$extended_class_file";;
esac
done
}
因此,使用上面的代码,运行时间从大约8分钟减少到2.5分钟。 相当不错。
如果有人可以提出任何其他变化,我将不胜感激。
将更多命令放入一个perl脚本中,例如
check_for_customization(){
filename="$1" extended_class_file="$2" perl -n - "$1" <<\EOF
next if /^\s*<!--/;
next unless /^.*class\s*([^\s]+).*/; $classname = $1;
next unless /^.*extends\s*([^\s]+).*/; $extended_classname = $1;
if (index($extended_classname, $classname) != -1)
{
print "$ENV{filename}\n";
open FILEOUT, ">>$ENV{extended_class_file}";
print FILEOUT "$extended_classname |$classname | $ENV{filename}\n"
}
EOF
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.