Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
331 views
in Technique[技术] by (71.8m points)

.net - Efficient method to print the content of an Ienumerable

I have this "result" var which is a IEnumerable type:

Dim result = GetCombinations(TextBox1.Text, StringLength)

To get/write the content of the variable I need to iterate all the items inside using a For and then convert each item to an array, like this:

    For Each item In result
        RichTextBox1.Text += vbNewLine & item.ToArray
        Application.DoEvents()
    Next

...So my answer is If I can improve my code for example to join the IEnumerable content to do something like this else:

 RichTextBox1.Text = String.Join(vbNewLine, result) ' This does not work.

I mean, a "in one go" thing.

If not, any alternative better (faster) than the For?

UPDATE

This is the full code:

Private Shared Function GetCombinations(Of T)(list As IEnumerable(Of T), length As Integer) As IEnumerable(Of IEnumerable(Of T))

    If length = 1 Then
        Return list.[Select](Function(x) New T() {x})
    Else
        Return GetCombinations(list, length - 1).SelectMany(Function(x) list, Function(t1, t2) t1.Concat(New T() {t2}))
    End If

End Function

   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    RichTextBox1.Clear()

    Dim result = GetCombinations("abc", 5)

    ' Dim result2 As IEnumerable(Of String) = result.Select(Function(item) New String(item))

    ' RichTextBox1.Text = String.Join(vbNewLine, result)

    For Each item In result
        RichTextBox1.Text &= vbNewLine & item.ToArray
        '  Application.DoEvents()
    Next

End Sub

UPDATE 2

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    ' Method result
    Dim result As IEnumerable = Permute_Characters("abc", 2)

    ' Combine strings into lines
    ' Dont work
    RichTextBox1.Text = String.Join(Environment.NewLine, result.ToString.ToArray)

End Sub
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Every time you update RichTextBox.Text property, you're probably going to incur an overhead as it does its own internal logic. Also, you're reading the value first so double that overhead right there.

Instead, I would use a StringBuilder in the loop. You can use the Append method overload of StringBuilder to add your characters to it. This overload accepts a character array.

Finally once your StringBuilder is built, then you can call RichTextBox1.Text = mystringbuilder.ToString().

This way you would avoid calling the "Clear" method of rich textbox as well as reading and reassigning the text property of it for every line. You will also avoid creating excessive amounts of string copies in memory that get generated when you do string concatenation operations.

EDIT PER COMMENTS: To do this in a single line without looping, you could use the LINQ extension function Aggregate. I use C# but you can use the VB syntax for this.

var v = GetCombinations(...);
RichTextBox1.Text = v.Aggregate((str, p) => str + Environment.NewLine + p);

But this still does string concatenations so I still suggest using a StringBuilder.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...