|
1 | 1 | --- |
2 | 2 | title: What's New in PowerShell 7.5 |
3 | 3 | description: New features and changes released in PowerShell 7.5 |
4 | | -ms.date: 11/18/2024 |
| 4 | +ms.date: 12/05/2024 |
5 | 5 | --- |
6 | 6 |
|
7 | 7 | # What's New in PowerShell 7.5 |
@@ -151,6 +151,102 @@ The following experimental features are included in PowerShell 7.5-rc.1: |
151 | 151 | - [PSSerializeJSONLongEnumAsNumber][06] - `ConvertTo-Json` now treats large enums as numbers |
152 | 152 | ([#20999][20999]) (Thanks @jborean93!) |
153 | 153 |
|
| 154 | +## Performance improvements |
| 155 | + |
| 156 | +PowerShell 7.5-rc.1 included [PR#23901][23901] from @jborean93 that improves the performance of the |
| 157 | +`+=` operation for an array of objects. |
| 158 | + |
| 159 | +The following example measures the performance for different methods of adding elements to an array. |
| 160 | + |
| 161 | +```powershell |
| 162 | +$tests = @{ |
| 163 | + 'Direct Assignment' = { |
| 164 | + param($count) |
| 165 | +
|
| 166 | + $result = foreach($i in 1..$count) { |
| 167 | + $i |
| 168 | + } |
| 169 | + } |
| 170 | + 'List<T>.Add(T)' = { |
| 171 | + param($count) |
| 172 | +
|
| 173 | + $result = [Collections.Generic.List[int]]::new() |
| 174 | + foreach($i in 1..$count) { |
| 175 | + $result.Add($i) |
| 176 | + } |
| 177 | + } |
| 178 | + 'Array+= Operator' = { |
| 179 | + param($count) |
| 180 | +
|
| 181 | + $result = @() |
| 182 | + foreach($i in 1..$count) { |
| 183 | + $result += $i |
| 184 | + } |
| 185 | + } |
| 186 | +} |
| 187 | +
|
| 188 | +5kb, 10kb | ForEach-Object { |
| 189 | + $groupResult = foreach($test in $tests.GetEnumerator()) { |
| 190 | + $ms = (Measure-Command { & $test.Value -Count $_ }).TotalMilliseconds |
| 191 | +
|
| 192 | +[pscustomobject]@{ |
| 193 | + CollectionSize = $_ |
| 194 | + Test = $test.Key |
| 195 | + TotalMilliseconds = [math]::Round($ms, 2) |
| 196 | + } |
| 197 | +
|
| 198 | +[GC]::Collect() |
| 199 | + [GC]::WaitForPendingFinalizers() |
| 200 | + } |
| 201 | +
|
| 202 | +$groupResult = $groupResult | Sort-Object TotalMilliseconds |
| 203 | + $groupResult | Select-Object *, @{ |
| 204 | + Name = 'RelativeSpeed' |
| 205 | + Expression = { |
| 206 | + $relativeSpeed = $_.TotalMilliseconds / $groupResult[0].TotalMilliseconds |
| 207 | + $speed = [math]::Round($relativeSpeed, 2).ToString() + 'x' |
| 208 | + if ($speed -eq '1x') { $speed } else { $speed + ' slower' } |
| 209 | + } |
| 210 | + } | Format-Table -AutoSize |
| 211 | +} |
| 212 | +``` |
| 213 | + |
| 214 | +When you run the script in PowerShell 7.4.6, you see that using the `+=` operator is the slowest |
| 215 | +method. |
| 216 | + |
| 217 | +```Output |
| 218 | +CollectionSize Test TotalMilliseconds RelativeSpeed |
| 219 | +-------------- ---- ----------------- ------------- |
| 220 | + 5120 Direct Assignment 4.17 1x |
| 221 | + 5120 List<T>.Add(T) 90.79 21.77x slower |
| 222 | + 5120 Array+= Operator 342.58 82.15x slower |
| 223 | +
|
| 224 | +
|
| 225 | +CollectionSize Test TotalMilliseconds RelativeSpeed |
| 226 | +-------------- ---- ----------------- ------------- |
| 227 | + 10240 Direct Assignment 0.64 1x |
| 228 | + 10240 List<T>.Add(T) 184.10 287.66x slower |
| 229 | + 10240 Array+= Operator 1668.13 2606.45x slower |
| 230 | +``` |
| 231 | + |
| 232 | +When you run the script in PowerShell 7.5-rc.1, you see that using the `+=` operator is much faster |
| 233 | +than PowerShell 7.4.6. Now, it's also faster than using the `List<T>.Add(T)` method. |
| 234 | + |
| 235 | +```Output |
| 236 | +CollectionSize Test TotalMilliseconds RelativeSpeed |
| 237 | +-------------- ---- ----------------- ------------- |
| 238 | + 5120 Direct Assignment 4.71 1x |
| 239 | + 5120 Array+= Operator 40.42 8.58x slower |
| 240 | + 5120 List<T>.Add(T) 92.17 19.57x slower |
| 241 | +
|
| 242 | +
|
| 243 | +CollectionSize Test TotalMilliseconds RelativeSpeed |
| 244 | +-------------- ---- ----------------- ------------- |
| 245 | + 10240 Direct Assignment 1.76 1x |
| 246 | + 10240 Array+= Operator 104.73 59.51x slower |
| 247 | + 10240 List<T>.Add(T) 173.00 98.3x slower |
| 248 | +``` |
| 249 | + |
154 | 250 | <!-- end of content --> |
155 | 251 | <!-- reference links --> |
156 | 252 | [chg]: https://github.com/PowerShell/PowerShell/blob/master/CHANGELOG/preview.md |
|
0 commit comments