|
| 1 | +# Function to simulate processing each line |
| 2 | +function Test-LineProcessing { |
| 3 | + param([string]$InputText) |
| 4 | + # Simulate some work by creating a simple string operation |
| 5 | + $InputText.Length > 0 |
| 6 | +} |
| 7 | + |
| 8 | +$tests = @{ |
| 9 | + 'Get-Content + foreach' = { |
| 10 | + param($filePath) |
| 11 | + $content = Get-Content -Path $filePath |
| 12 | + foreach ($line in $content) { |
| 13 | + Test-LineProcessing -InputText $line |
| 14 | + } |
| 15 | + } |
| 16 | + 'Get-Content | ForEach-Object' = { |
| 17 | + param($filePath) |
| 18 | + Get-Content -Path $filePath | |
| 19 | + ForEach-Object -Process { |
| 20 | + Test-LineProcessing -InputText $_ |
| 21 | + } |
| 22 | + } |
| 23 | + 'StreamReader' = { |
| 24 | + param($filePath) |
| 25 | + $sr = New-Object -TypeName System.IO.StreamReader -ArgumentList $filePath |
| 26 | + try { |
| 27 | + while ($sr.Peek() -ge 0) { |
| 28 | + $line = $sr.ReadLine() |
| 29 | + Test-LineProcessing -InputText $line |
| 30 | + } |
| 31 | + } finally { |
| 32 | + $sr.Dispose() |
| 33 | + } |
| 34 | + } |
| 35 | + 'Get-Content -ReadCount 1' = { |
| 36 | + param($filePath) |
| 37 | + Get-Content -Path $filePath -ReadCount 1 | |
| 38 | + ForEach-Object -Process { |
| 39 | + Test-LineProcessing -InputText $_ |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +# Create test files |
| 45 | +$testFiles = @{ |
| 46 | + 'test-small.txt' = (1..100 | ForEach-Object { "This is line $_ with some additional text to make it realistic." }) |
| 47 | + 'test-medium.txt' = (1..5000 | ForEach-Object { |
| 48 | + "This is line $_ with some additional text to make it realistic and longer for testing purposes." |
| 49 | + }) |
| 50 | + 'test-large.txt' = (1..50000 | ForEach-Object { |
| 51 | + "This is line $_ with some additional text to make it realistic and longer for testing purposes with even more content." |
| 52 | + }) |
| 53 | +} |
| 54 | + |
| 55 | +# Generate test files |
| 56 | +foreach ($file in $testFiles.GetEnumerator()) { |
| 57 | + $file.Value | Out-File -FilePath $file.Key -Encoding UTF8 |
| 58 | +} |
| 59 | + |
| 60 | +'test-small.txt', 'test-medium.txt', 'test-large.txt' | ForEach-Object { |
| 61 | + $groupResult = foreach ($test in $tests.GetEnumerator()) { |
| 62 | + $ms = (Measure-Command { & $test.Value $_ }).TotalMilliseconds |
| 63 | + |
| 64 | + [pscustomobject]@{ |
| 65 | + TestFile = $_ |
| 66 | + Test = $test.Key |
| 67 | + TotalMilliseconds = [math]::Round($ms, 2) |
| 68 | + } |
| 69 | + |
| 70 | + [GC]::Collect() |
| 71 | + [GC]::WaitForPendingFinalizers() |
| 72 | + } |
| 73 | + |
| 74 | + $groupResult = $groupResult | Sort-Object TotalMilliseconds |
| 75 | + $groupResult | Select-Object *, @{ |
| 76 | + Name = 'RelativeSpeed' |
| 77 | + Expression = { |
| 78 | + $relativeSpeed = $_.TotalMilliseconds / $groupResult[0].TotalMilliseconds |
| 79 | + [math]::Round($relativeSpeed, 2).ToString() + 'x' |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +# Cleanup test files |
| 85 | +'test-small.txt', 'test-medium.txt', 'test-large.txt' | ForEach-Object { |
| 86 | + Remove-Item $_ -Force -ErrorAction SilentlyContinue |
| 87 | +} |
0 commit comments