paulvill76

Untitled

Oct 19th, 2021
694
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #I have one project where I am importing a csv file and then I need to change the date format in two columns.
  2.  
  3. #I have completed few following loops where I am changing that format. So far for now, the fastest loop is the first one with ArrayList.
  4.  
  5. #Yes It's fast if you are working with hundreds or thousands records, but luckily I am working with ~200k records which takes around 2 minutes.
  6.  
  7. #So I just wanted to ask you if you know some another possibility how to do it.
  8.  
  9. $path = "$([Environment]::GetFolderPath('Desktop'))\SomeCSVFile.csv"
  10.  
  11. $RawCsv = Import-Csv -Path $path -Header ID, stime, etime, Process, User, Resource, FR
  12.  
  13. $Csv = $RawCsv | Select-Object -First 50000
  14.  
  15. $Stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
  16.  
  17. ########################################################################
  18.  
  19. $Stopwatch.Restart()
  20. $arr = [System.Collections.ArrayList]::new()
  21. foreach ($row in $csv) {
  22.     $object = [PSCustomObject]@{
  23.         ID        = $row.ID
  24.         StartTime = (Get-Date $row.stime -Format 'yyyy-MM-dd HH:mm:ss')
  25.         EndTime   = (Get-Date $row.etime -Format 'yyyy-MM-dd HH:mm:ss')
  26.         Process   = $row.Process
  27.         User      = $row.User
  28.         Resource  = $row.Resource
  29.     }
  30.     [void]$arr.Add($object)
  31. }
  32. Write-Host "Array completed in: $($Stopwatch.Elapsed.TotalSeconds)"
  33.  
  34. ########################################################################
  35.  
  36. $Stopwatch.Restart()
  37. $out = foreach ($row in $csv) {
  38.     [PSCustomObject]@{
  39.         ID        = $row.ID
  40.         StartTime = (Get-Date $row.stime -Format 'yyyy-MM-dd HH:mm:ss')
  41.         EndTime   = (Get-Date $row.etime -Format 'yyyy-MM-dd HH:mm:ss')
  42.         Process   = $row.Process
  43.         User      = $row.User
  44.         Resource  = $row.Resource
  45.     }
  46. }
  47. Write-Host "Object completed in: $($Stopwatch.Elapsed.TotalSeconds)"
  48.  
  49. ########################################################################
  50.  
  51. $Stopwatch.Restart()
  52. foreach ($row in $csv) {
  53.     $row.stime = (Get-Date $row.stime -Format 'yyyy-MM-dd HH:mm:ss')
  54.     $row.etime = (Get-Date $row.etime -Format 'yyyy-MM-dd HH:mm:ss')
  55. }
  56. Write-Host "Update completed in: $($Stopwatch.Elapsed.TotalSeconds)"
  57.  
  58. ########################################################################
  59.  
  60. $Stopwatch.Restart()
  61. $csv.ForEach({ $_.stime = (Get-Date $_.stime -Format 'yyyy-MM-dd HH:mm:ss'); $_.etime = (Get-Date $_.etime -Format 'yyyy-MM-dd HH:mm:ss') })
  62. Write-Host "DotForeach completed in: $($Stopwatch.Elapsed.TotalSeconds)"
  63.  
  64. ########################################################################
  65.  
  66. $Stopwatch.Stop()
RAW Paste Data