Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- This is in reply to a question by hdsouza on January 18, 2018 at 7:52 pm at following url:
- https://blogs.technet.microsoft.com/ashleymcglone/2017/07/12/slow-code-top-5-ways-to-make-your-powershell-scripts-run-faster/
- #>
- $File_NotesDownload = 'C:\temp\SecondaryMarketAllNotes.csv'
- $File_NotesGood = 'C:\temp\NotesGood.csv'
- if (Test-Path $File_NotesGood) { Remove-Item $File_NotesGood }
- $Start = Get-Date
- <#
- With Get-Content you can use the Raw parameter to speed up reading of large files.
- However, let's use Import-Csv cmdlet instead.
- #>
- #$Notes = Get-Content $File_NotesDownload -Raw
- <#
- I gathered from the original code that this is a comma-delimited csv, but it does not have headers on its first row.
- As such, we have to specify the headers by using the Header parameter, like so.
- Please fill in any missing headers, and make sure they are in the same order as in the csv file.
- By wrapping Import-Csv within @() ensures that Notes variable is always an array, even if SecondaryMarketAllNotes.csv only contains a single row.
- #>
- $Notes = @(Import-Csv $File_NotesDownload -Delimiter ',' -Header 'Loan_Id',
- 'Loan_Status',
- 'Ask_price',
- 'Markup_Discount',
- 'YTM',
- 'DaysSinceLastPayment',
- 'CreditScoreTrend',
- 'CreditScoreTrend',
- 'FicoEndRange',
- 'NeverLate',
- 'Loan_Class',
- 'Loan_Maturity',
- 'Interest_Rate',
- 'RemainingPayments')
- <#
- If count of objects stored in ArrayList exceed capacity of the ArrayList, .NET creates a new ArrayList with increased capacity and copies objects over to the new ArrayList.
- This is exact same behavior as with immutable arrays, only that capacity is doubled each time which makes reallocations far fewer than it would be with arrays.
- However, it is possible to instantiate ArrayList with given capacity that in ideal case is just right.
- In this case, you never store more objects in ArrayList than what Notes already contains.
- We can use number of objects stored in Notes as immediate capacity for NotesList.
- #>
- $NotesList = [System.Collections.ArrayList]::new($Notes.Count)
- foreach ($Note in $Notes) {
- if (($Note.Loan_Id -is [int]) -eq $false) {Continue}
- if ($Note.Loan_Maturity -ne 36) {Continue}
- if (($Note.Interest_Rate -lt 4) -Or ($Interest_Rate -gt 25)) {Continue}
- if ($Note.Ask_price -gt 20) {Continue}
- if ($Note.Markup_Discount -gt -0.01) {Continue}
- if ($Note.YTM -lt 10) {Continue}
- if ($Note.CredScoretrend -ne 'UP') {Continue}
- # …..I have more filters to add ……
- # By using [void] we prevent Add method from outputting index of added item to console.
- [void]$NotesList.Add($Note)
- }
- <#
- In the original code output was into a txt file, but I changed it to csv because even in the original code output was essentially in csv format.
- We can use Export-Csv to conveniently output csv files.
- #>
- $NotesList | Export-Csv $File_NotesGood -NoTypeInformation -Delimiter ','
- $End = Get-Date
- $TotalTime = New-Timespan -Start $Start -End $End
- $TotalTime.Seconds
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement