How can I make this PowerShell script parse large files faster?

后端 未结 3 628
名媛妹妹
名媛妹妹 2020-12-01 12:58

I have the following PowerShell script that will parse some very large file for ETL purposes. For starters my test file is ~ 30 MB. Larger files around 200 MB are

3条回答
  •  失恋的感觉
    2020-12-01 13:33

    The Get-Content cmdlet does not perform as well as a StreamReader when dealing with very large files. You can read a file line by line using a StreamReader like this:

    $path = 'C:\A-Very-Large-File.txt'
    $r = [IO.File]::OpenText($path)
    while ($r.Peek() -ge 0) {
        $line = $r.ReadLine()
        # Process $line here...
    }
    $r.Dispose()
    

    Some performance comparisons:

    Measure-Command {Get-Content .\512MB.txt > $null}
    

    Total Seconds: 49.4742533

    Measure-Command {
        $r = [IO.File]::OpenText('512MB.txt')
        while ($r.Peek() -ge 0) {
            $r.ReadLine() > $null
        }
        $r.Dispose()
    }
    

    Total Seconds: 27.666803

提交回复
热议问题