KelvinTegelaar / AzPwPush

11 stars 11 forks source link

Performance - .net ReadAllLines instead of Import-CSV #1

Closed itfranck closed 3 years ago

itfranck commented 3 years ago

Hey @KelvinTegelaar, thank you for making this.

Regarding the wordlist.txt loading, you can do things even faster !

Iteration run 100 times image

Get-Content without the Raw parameter is notoriously slow. Get-Content -Raw is very fast but converting it to a CSV after is just a tiny bit slower than Import-CSV

That being said, you don't have a csv, you have a list of string. Thus, you can use Get-Content -Raw (very fast) and split by lines.

$words  =(Get-Content -Raw -Path .\wordlist.txt).split("`n")

For even better performance, you can use .net with

$words = [System.IO.File]::ReadAllLines('wordlist.txt')

Here is what I used to calculate these results.

$Path = 'wordlist.txt'
$Iteration = 100

Write-Host "Get-Content as an array ".PadRight(30,' ') -NoNewline -ForegroundColor Cyan
$ms = Measure-Command {
    1..$Iteration | % {$words  =Get-Content -Path $Path} 
} | Select -ExpandProperty TotalMilliseconds 
Write-Host "($ms ms)"

Write-Host "Get-Content raw as CSV".PadRight(30,' ') -NoNewline -ForegroundColor Cyan
$ms = Measure-Command {
    1..$Iteration | % {$words  =Get-Content -Path $Path -Raw | ConvertFrom-Csv} 
} | Select -ExpandProperty TotalMilliseconds 
Write-Host "($ms ms)"

Write-Host "Import CSV".PadRight(30,' ') -NoNewline -ForegroundColor Cyan
$ms = Measure-Command {
    1..$Iteration | % {$words  =Import-Csv -Path $Path} 
} | Select -ExpandProperty TotalMilliseconds 
Write-Host "($ms ms)"

Write-Host "Get-Content -Raw + Split".PadRight(30,' ') -NoNewline -ForegroundColor Cyan
$ms = Measure-Command {
    1..$Iteration| % {$words  =(Get-Content -Raw -Path $Path).split("`n")} 
} | Select -ExpandProperty TotalMilliseconds 
Write-Host "($ms ms)"

Write-Host ".net ReadAllLines".PadRight(30,' ') -NoNewline -ForegroundColor Cyan
$ms = Measure-Command {
    1..$Iteration| % {
        $Words = [System.IO.file]::ReadAllLines($Path)
    } 
} | Select -ExpandProperty TotalMilliseconds 
Write-Host "($ms ms)"
KelvinTegelaar commented 3 years ago

Didn't really have performance in mind when building it, as it was more of a POC at the start, Lets merge :)