question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Performance: -join slower than [string]::Join()

See original GitHub issue

Steps 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
iSazonovcommented, Aug 20, 2019

I pulled PR for $w2 = ${D:\test\wordlist.txt}; scenario but it seems also fix the first scenario for random numbers.

1reaction
SeeminglySciencecommented, Aug 20, 2019

I very wonder that results for $w1 and $w2 notably differ

The former is a object[] containing PSObject’s, the latter is a object[] containing string’s.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found