use*_*975 3 powershell performance
我有一个非常大的文本文件,大小为250 GB,由供应商提供给我们.它们还为我们提供了一个控制文件,该文件应该包含大文件中的行数.有时会出现不匹配的情况.我如何计算Powershell中的线条?我尝试了这个命令,它运行了半个多小时,但还没有完成.
Get-content C:\test.txt | Measure-Object –Line
(gc C:\test.txt | Measure-object | select count).count
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏谢谢MR
dha*_*ech 11
我有一个 1.39 GB 的 csv 文件:
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 10/4/2021 1:23 PM 1397998768 XBTUSD.csv
Run Code Online (Sandbox Code Playgroud)
wc -l
在 WSL 中。通过 访问文件/mnt/c/Users
。
速度较慢,因为文件位于 NTFS 端。
$ time wc -l XBTUSD.csv
41695261 XBTUSD.csv
real 0m10.935s
user 0m0.951s
sys 0m1.427s
Run Code Online (Sandbox Code Playgroud)
wc -l
在 WSL 中。文件位于/tmp
.
time wc -l /tmp/XBTUSD.csv
41695261 /tmp/XBTUSD.csv
real 0m0.447s
user 0m0.258s
sys 0m0.189s
Run Code Online (Sandbox Code Playgroud)
Measure-Command { Get-Content .\XBTUSD.csv | Measure-Object -Line }
Days : 0
Hours : 0
Minutes : 7
Seconds : 52
Milliseconds : 353
Ticks : 4723537381
TotalDays : 0.00546705715393518
TotalHours : 0.131209371694444
TotalMinutes : 7.87256230166667
TotalSeconds : 472.3537381
TotalMilliseconds : 472353.7381
Run Code Online (Sandbox Code Playgroud)
ReadLines
以及Count
Measure-Command {
[System.Linq.Enumerable]::Count(
[System.IO.File]::ReadLines((ls .\XBTUSD.csv).FullName))
}
Run Code Online (Sandbox Code Playgroud)
Days : 0
Hours : 0
Minutes : 0
Seconds : 7
Milliseconds : 263
Ticks : 72636842
TotalDays : 8.40704189814815E-05
TotalHours : 0.00201769005555556
TotalMinutes : 0.121061403333333
TotalSeconds : 7.2636842
TotalMilliseconds : 7263.6842
Run Code Online (Sandbox Code Playgroud)
Measure-Command {
$count = 0; switch -File .\XBTUSD.csv { default { ++$count } }; $count
}
Run Code Online (Sandbox Code Playgroud)
Days : 0
Hours : 0
Minutes : 0
Seconds : 44
Milliseconds : 975
Ticks : 449752555
TotalDays : 0.000520546938657407
TotalHours : 0.0124931265277778
TotalMinutes : 0.749587591666667
TotalSeconds : 44.9752555
TotalMilliseconds : 44975.2555
Run Code Online (Sandbox Code Playgroud)
最快的是wc -l
WSL。
PowerShell 中最快的是 LINQ 方法
如果性能很重要,请避免使用cmdlet和管道; 用途switch -File
:
$count = 0
switch -File C:\test.txt {
default { ++$count }
}
Run Code Online (Sandbox Code Playgroud)
switch -File
枚举指定文件的行; 条件default
匹配任何行.
要了解性能差异:
# Create a sample file with 100,000 lines.
1..1e5 > tmp.txt
# Warm up the file cache
foreach ($line in [IO.File]::ReadLines("$pwd/tmp.txt")) { }
(Measure-Command { (Get-Content tmp.txt | Measure-Object).Count }).TotalSeconds
(Measure-Command { $count = 0; switch -File tmp.txt { default { ++$count } } }).TotalSeconds
Run Code Online (Sandbox Code Playgroud)
我的Windows 10/PSv5.1机器的示例结果:
1.3081307 # Get-Content + Measure-Object
0.1097513 # switch -File
Run Code Online (Sandbox Code Playgroud)
也就是说,在我的机器上,switch -File
命令速度提高了大约12倍.
对于如此大的文件,我宁愿使用一些 C 编写的实用程序。安装gitbash,它应该有wc命令:
wc -l yourfile.txt
Run Code Online (Sandbox Code Playgroud)
我在5GB/50M行文件(HDD上)上进行了测试,大约需要40s。最好的 powershell 解决方案大约需要 2 分钟。您还可以检查您的文件,它可能有一些自动增量索引或恒定的行大小。
归档时间: |
|
查看次数: |
488 次 |
最近记录: |