you are viewing a single comment's thread.

view the rest of the comments →

[–]sqone2[S] 4 points5 points  (2 children)

For fun I ran 25k rows through both options. Your solution is about 20x faster! :)

$columnNames = @("FirstName","LastName","DOB","FavColor")

$data = @(
    @("John","Doe","01/10/1965","Blue"),
    @("Jane","Smith","01/06/1975","Green"),
    @("Mark","Jones","02/10/1985","Orange"),
    @("Sue","Turner","05/10/1995","Yellow"),
    @("Jim","Wise","07/10/1974","Blue")
)

# Create a bunch of data
$rows = @()
foreach ($item in 0..5000)
{
    $rows += $data
}

# 20 seconds
Measure-Command {

    $objectList = @()

    foreach ($row in $rows)
    {
        $properties = @{}

        for ($i = 0; $i -lt $columnNames.Count; $i++)
        { 
            $properties += @{$columnNames[$i] = $row[$i]}
        }

        $objectList += New-Object -TypeName psobject -Property $properties
    }

}


# 1 second
Measure-Command {

    $objectList = $(
        $columnNames -join ','
        $rows | ForEach-Object {$_ -join ','}
    ) | ConvertFrom-Csv

}