Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #I have one project where I am importing a csv file and then I need to change the date format in two columns.
- #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.
- #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.
- #So I just wanted to ask you if you know some another possibility how to do it.
- $path = "$([Environment]::GetFolderPath('Desktop'))\SomeCSVFile.csv"
- $RawCsv = Import-Csv -Path $path -Header ID, stime, etime, Process, User, Resource, FR
- $Csv = $RawCsv | Select-Object -First 50000
- $Stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
- ########################################################################
- $Stopwatch.Restart()
- $arr = [System.Collections.ArrayList]::new()
- foreach ($row in $csv) {
- $object = [PSCustomObject]@{
- ID = $row.ID
- StartTime = (Get-Date $row.stime -Format 'yyyy-MM-dd HH:mm:ss')
- EndTime = (Get-Date $row.etime -Format 'yyyy-MM-dd HH:mm:ss')
- Process = $row.Process
- User = $row.User
- Resource = $row.Resource
- }
- [void]$arr.Add($object)
- }
- Write-Host "Array completed in: $($Stopwatch.Elapsed.TotalSeconds)"
- ########################################################################
- $Stopwatch.Restart()
- $out = foreach ($row in $csv) {
- [PSCustomObject]@{
- ID = $row.ID
- StartTime = (Get-Date $row.stime -Format 'yyyy-MM-dd HH:mm:ss')
- EndTime = (Get-Date $row.etime -Format 'yyyy-MM-dd HH:mm:ss')
- Process = $row.Process
- User = $row.User
- Resource = $row.Resource
- }
- }
- Write-Host "Object completed in: $($Stopwatch.Elapsed.TotalSeconds)"
- ########################################################################
- $Stopwatch.Restart()
- foreach ($row in $csv) {
- $row.stime = (Get-Date $row.stime -Format 'yyyy-MM-dd HH:mm:ss')
- $row.etime = (Get-Date $row.etime -Format 'yyyy-MM-dd HH:mm:ss')
- }
- Write-Host "Update completed in: $($Stopwatch.Elapsed.TotalSeconds)"
- ########################################################################
- $Stopwatch.Restart()
- $csv.ForEach({ $_.stime = (Get-Date $_.stime -Format 'yyyy-MM-dd HH:mm:ss'); $_.etime = (Get-Date $_.etime -Format 'yyyy-MM-dd HH:mm:ss') })
- Write-Host "DotForeach completed in: $($Stopwatch.Elapsed.TotalSeconds)"
- ########################################################################
- $Stopwatch.Stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement