Results 1 to 6 of 6

Thread: [RESOLVED] Incremental generation is slow even for small values

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    Resolved [RESOLVED] Incremental generation is slow even for small values

    Hi,
    I am currently using this code to generate sequential numbers separated by space. Numbers are small (starting from 254125, ending in 255124). Works perfectly apart from one big issue - generation is very slow even I am using small numbers. How can it be speeded up? Thanks.

    Code:
    Imports System.Numerics
    Public Class ArithmeticSequenceGenerator
    
        Dim number As BigInteger = 1
        Dim firstTerm As BigInteger = 0
        Dim commondifference As BigInteger = 0
        Dim terms As BigInteger = 0
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim k As BigInteger
            firstTerm = TextBox1.Text
            commondifference = TextBox2.Text
            terms = TextBox3.Text
            TextBox4.Clear()
            For k = 1 To terms
                number = firstTerm + commondifference * (k - 1)
                TextBox4.Text += number.ToString + " "
            Next
        End Sub
    End Class
    Please dont forget to add good reputation if my advices were useful for you.
    How? Under this post there is "RATE THIS POST" button. Click on it.

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Incremental generation is slow even for small values

    The way you are doing it now - updating TextBox4.Text for every iteration of the loop - is extremely inefficient for a multitude of reasons.

    Use a StringBuilder object to build your concatenated string inside of your loop and then assign that to TextBox4.Text after the loop is finished.

    https://docs.microsoft.com/en-us/dot...r?view=net-5.0

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Incremental generation is slow even for small values

    I was going to say the same thing.

    String concatenation is far slower than you might expect. Strings can't change size, so every time you use += (&= would be better, but the same speed), you are creating a new string of the new size, moving everything from the existing string to the new one, then abandoning the old one. You are doing that a thousand times, in your example. That will KILL performance.

    The StringBuilder is a mutable string, so it doesn't do quite so much thrashing as what you are doing now. That's what makes it faster.
    My usual boring signature: Nothing

  4. #4
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Incremental generation is slow even for small values

    Also, since you have struggled with assigning values to a BigInteger object in numerous past threads over the course of the past couple years, and have been shown the proper way to assign values to BigInteger objects in numerous past threads over the past couple years, it is extremely disappointing to see you still do silly things like this:

    Code:
        Dim firstTerm As BigInteger = 0
        Dim commondifference As BigInteger = 0
        Dim terms As BigInteger = 0
    
        firstTerm = TextBox1.Text
        commondifference = TextBox2.Text
        terms = TextBox3.Text

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    Re: Incremental generation is slow even for small values

    OB1,
    thank you for pointing me for SB. Now it works pretty fast - even for 10 000 values!

    Shaggy,
    thanks for explanation of strings.
    Please dont forget to add good reputation if my advices were useful for you.
    How? Under this post there is "RATE THIS POST" button. Click on it.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [RESOLVED] Incremental generation is slow even for small values

    No need to even use a StringBuilder. Just do something like this:
    vb.net Code:
    1. Dim numbers = Enumerable.Range(1, 10).Select(Function(n) n * 10 + 1)
    2.  
    3. TextBox4.Text = String.Join(" ", numbers)
    The range can be whatever you need, as can be the calculation. I didn't bother trying to determine exactly what they'd be in your case because you can do that with a bit of logic and a bit of testing. I know that you're using BigInteger in the code you posted but I can't see any specific reason for that in what you posted. That said, as long as you can express the range using Integers, the calculation can still produce BigIntegers if that's what you need.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width