Performance: -join slower than [string]::Join()
See original GitHub issueSteps to reproduce
Joining 100kb random numbers, the operator is much slower, ~13x here:
[string[]]$nums = Get-Random -Minimum 1 -Maximum 100 -Count 100kb
measure-command { $nums -join ',' } |% TotalMilliseconds
measure-command { [string]::join(',', $nums) } |% TotalMilliseconds
234.82690000000002
14.497100000000001
Stranger, with a big wordlist text file, loading it two different ways seems to make a difference:
$w1 = Get-Content d:\test\wordlist.txt; $w1.Count
$w2 = ${D:\test\wordlist.txt}; $w2.Count
measure-command { $w1 -join "`r`n" } |% TotalMilliseconds
measure-command { $w2 -join "`r`n" } |% TotalMilliseconds
measure-command { [string]::join("`r`n", $w1) } |% TotalMilliseconds
measure-command { [string]::join("`r`n", $w2) } |% TotalMilliseconds
172823
172823 # same word count
254.6534
423.63100000000003 # <-- slower?
183.01510000000002
16.148400000000002 # <-- how come this one is SO fast?
Environment data
PS D:\> $PSVersionTable
Name Value
---- -----
PSVersion 7.0.0-preview.1
PSEdition Core
GitCommitId 7.0.0-preview.1
OS Microsoft Windows 10.0.17763
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:11 (1 by maintainers)
Top Results From Across the Web
How slow is Python's string concatenation vs. str.join?
String join is significantly faster then concatenation. Why? Strings are immutable and can't be changed in place. To alter one, a new ...
Read more >Are int joins faster than string joins?
This is to answer the 'string joins are slower because they are larger' argument.
Read more >Are int joins faster than string joins?
This is to answer the 'string joins are slower because they are larger' argument.
Read more >Performance Of String Concatenation In C# – .NET Core ...
Join is fast(er) with StringBuilder being the clear leader. String.Format is slowest by a mile. What's going on here? We are going to...
Read more >How Do Column Types Affect Join Speeds In Data ...
Logically speaking, integers must be faster than strings and byte-strings because there are generally fewer bytes to scan. But… by how much!?
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I pulled PR for
$w2 = ${D:\test\wordlist.txt};
scenario but it seems also fix the first scenario for random numbers.The former is a
object[]
containingPSObject
’s, the latter is aobject[]
containingstring
’s.