我正在获取文件的内容,并仅保留与正则表达式或空行匹配的行。但是写结果,例如数据量较小,需要花费很多时间... 这是有问题的代码(我添加了几行用于调试/测量):
$original = Get-Content "$localDir\$ldif_file"
(Measure-Command -Expression { $original | Out-File "$localDir\Original-$ldif_file" }).TotalSeconds
$lines = ($original | Measure-Object -Line).Lines
"lines of `$original = $lines"
# Just keep lines of interest:
$stripped = $original | select-string -pattern '^custom[A-Z]','^$' -CaseSensitive
$lines = ($stripped | Measure-Object -Line).Lines
"lines of `$stripped = $lines"
(Measure-Command -Expression { $stripped | Out-File "$localDir\Stripped-$ldif_file" }).TotalSeconds
"done"
问题:将较小($ stripped)的数据写入文件需要342秒(比$原始数据长30倍)!参见下面的输出:
11.5371677
lines of $original = 188715
lines of $stripped = 126404
342.6769547
done
为什么$ str的Out-File比$ original的文件慢了这么多? 如何改进?
谢谢!