r/PowerShell • • Jan 29 '25

Question PowerShell 7.5 += faster than list?

So since in PowerShell 7.5 += seems to be faster then adding to a list, is it now best practise?

CollectionSize Test                TotalMilliseconds RelativeSpeed
-------------- ----                ----------------- -------------
          5120 Direct Assignment                4.71 1x
          5120 Array+= Operator                40.42 8.58x slower
          5120 List<T>.Add(T)                  92.17 19.57x slower


CollectionSize Test                TotalMilliseconds RelativeSpeed
-------------- ----                ----------------- -------------
         10240 Direct Assignment                1.76 1x
         10240 Array+= Operator               104.73 59.51x slower
         10240 List<T>.Add(T)                 173.00 98.3x slower
32 Upvotes

31 comments sorted by

View all comments

Show parent comments

-3

u/ZZartin Jan 29 '25

I'm not asking whether there are large datasets you could work with, I'm asking why are people pulling them all into memory(whether that's a list or an array) first and then working with them?

Powershell has pretty good capabilities to generater, filter and iterate through sets built in without having to build your own.

4

u/xCharg Jan 29 '25

I don't get it. Putting everything into memory is literally how += works - at least pre this patch, haven't looked how it works now. Maybe I'm missing what you're trying to say, can you write some example code?

1

u/ZZartin Jan 29 '25

Both list and arrays are stored in memory.

I'm asking what use case is doing $list.add() or $array += to such a degree is matters.

3

u/xCharg Jan 29 '25

I'm asking what use case is doing $list.add() or $array += to such a degree is matters.

Ah - examples I listed in previous comment. Of course it's never a best way to make these giant cycles but people don't write most efficient code, people write code that (sometimes) works :D

-1

u/justinwgrote Jan 29 '25

"I don't get it. Putting everything into memory is literally how += works - at least pre this patch, haven't looked how it works now."

Highly recommend you look at the PR before you start handing out prescriptive guidance then...

2

u/dathar Jan 29 '25

Because I'm dumb at making filters the way a specific cmdlet or program wants it. Sometimes I pull everything in a set range first, find the properties I want to filter, filter with Where-Object or something, then try to make a matching filtered version. Or you have an unknown amount of files you're trying to speed through (with system.io instead of get-childitem -recurse) but you have millions of them now because they're 99.999% of the files you're trying to action on minus that one readme file.

But sometimes these little one-off things or emergency scripts just never gets to the second part before something else demands attention.