Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2018
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.     This is in reply to a question by hdsouza on January 18, 2018 at 7:52 pm at following url:
  3.     https://blogs.technet.microsoft.com/ashleymcglone/2017/07/12/slow-code-top-5-ways-to-make-your-powershell-scripts-run-faster/
  4. #>
  5.  
  6. $File_NotesDownload = 'C:\temp\SecondaryMarketAllNotes.csv'
  7. $File_NotesGood = 'C:\temp\NotesGood.csv'
  8.  
  9. if (Test-Path $File_NotesGood) { Remove-Item $File_NotesGood }
  10.  
  11. $Start = Get-Date
  12.  
  13. <#
  14.     With Get-Content you can use the Raw parameter to speed up reading of large files.
  15.     However, let's use Import-Csv cmdlet instead.
  16. #>
  17. #$Notes = Get-Content $File_NotesDownload -Raw
  18.  
  19. <#
  20.     I gathered from the original code that this is a comma-delimited csv, but it does not have headers on its first row.
  21.     As such, we have to specify the headers by using the Header parameter, like so.
  22.     Please fill in any missing headers, and make sure they are in the same order as in the csv file.
  23.     By wrapping Import-Csv within @() ensures that Notes variable is always an array, even if SecondaryMarketAllNotes.csv only contains a single row.
  24. #>
  25.     $Notes = @(Import-Csv $File_NotesDownload -Delimiter ',' -Header 'Loan_Id',
  26.                                                                     'Loan_Status',
  27.                                                                     'Ask_price',
  28.                                                                     'Markup_Discount',
  29.                                                                     'YTM',
  30.                                                                     'DaysSinceLastPayment',
  31.                                                                     'CreditScoreTrend',
  32.                                                                     'CreditScoreTrend',
  33.                                                                     'FicoEndRange',
  34.                                                                     'NeverLate',
  35.                                                                     'Loan_Class',
  36.                                                                     'Loan_Maturity',
  37.                                                                     'Interest_Rate',
  38.                                                                     'RemainingPayments')
  39.  
  40. <#
  41.     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.
  42.     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.
  43.     However, it is possible to instantiate ArrayList with given capacity that in ideal case is just right.    
  44.     In this case, you never store more objects in ArrayList than what Notes already contains.
  45.     We can use number of objects stored in Notes as immediate capacity for NotesList.
  46. #>
  47. $NotesList = [System.Collections.ArrayList]::new($Notes.Count)
  48.  
  49. foreach ($Note in $Notes) {
  50.     if (($Note.Loan_Id -is [int]) -eq $false) {Continue}
  51.     if ($Note.Loan_Maturity -ne 36) {Continue}
  52.     if (($Note.Interest_Rate -lt 4) -Or ($Interest_Rate -gt 25)) {Continue}
  53.     if ($Note.Ask_price -gt 20) {Continue}
  54.     if ($Note.Markup_Discount -gt -0.01) {Continue}
  55.     if ($Note.YTM -lt 10) {Continue}
  56.     if ($Note.CredScoretrend -ne 'UP') {Continue}
  57.     # …..I have more filters to add ……
  58.  
  59.     # By using [void] we prevent Add method from outputting index of added item to console.
  60.     [void]$NotesList.Add($Note)
  61. }
  62.  
  63. <#
  64.     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.
  65.     We can use Export-Csv to conveniently output csv files.
  66. #>
  67. $NotesList | Export-Csv $File_NotesGood -NoTypeInformation -Delimiter ','
  68.  
  69. $End = Get-Date
  70. $TotalTime = New-Timespan -Start $Start -End $End
  71. $TotalTime.Seconds
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement