Results 1 to 19 of 19

Thread: Character insertion process is very slow

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2018
    Posts
    230

    Character insertion process is very slow

    Hi,
    I am facing slow insertion of characters into textbox (lot of lines there) using this code:
    Code:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick       
    Dim StrData As String
    StrData = TextBox4.Text
    StrData = Replace(StrData, " ", "A", , TextBox_A.Text)
    StrData = Replace(StrData, " ", "B", , TextBox_B.Text)
    TextBox4.Text = StrData
    End Sub
    I tried to resolve this slowness via string builder,

    Code:
    Dim sb As New System.Text.StringBuilder
    Dim StrData As String = sb.ToString
    StrData = TextBox4.Text
    StrData = Replace(StrData, " ", "A", , TextBox_A.Text)
    StrData = Replace(StrData, " ", "B", , TextBox_B.Text)
    sb.Append(StrData.ToString)
    Dim strdata2 As String = sb.ToString
    TextBox4.Text = strdata2.ToString
    but problem persist...
    Please dont forget to add good reputation if my advices were useful for you.

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,951

    Re: Character insertion process is very slow

    Haven't tried but, how about putting the characters to an array , replace them and put them back to the textbox.
    .

  3. #3
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,795

    Re: Character insertion process is very slow

    using a Timer seem's strange if you want to replace character/string?
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,252

    Re: Character insertion process is very slow

    I agree with ChrisE and this raises the question, what are you actually trying to do? Not what you've done, but what it is that you're trying to accomplish.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2018
    Posts
    230

    Re: Character insertion process is very slow

    Quote Originally Posted by ChrisE View Post
    using a Timer seem's strange if you want to replace character/string?
    Yep, but this is my form of a loop... writing loop is hard for me and thus I am using timers.
    Please dont forget to add good reputation if my advices were useful for you.

  6. #6
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,557

    Re: Character insertion process is very slow

    Quote Originally Posted by VB.NET Developer View Post
    Yep, but this is my form of a loop... writing loop is hard for me and thus I am using timers.
    That is the most unique reason I think I've ever seen anyone give for why they are using a timer.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Oct 2018
    Posts
    230

    Re: Character insertion process is very slow

    Quote Originally Posted by OptionBase1 View Post
    That is the most unique reason I think I've ever seen anyone give for why they are using a timer.
    Agree. But loops via timers works flawlessly in my other projects.
    Last edited by VB.NET Developer; Mar 16th, 2021 at 04:00 PM.
    Please dont forget to add good reputation if my advices were useful for you.

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,252

    Re: Character insertion process is very slow

    Don't do that. Please.

    I get that you don't want to use a loop, but you still haven't answered what it is that you do want to do.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Oct 2018
    Posts
    230

    Re: Character insertion process is very slow

    @dday9 see my first post. This thread is not about looping is better via timers etc topic.
    Please dont forget to add good reputation if my advices were useful for you.

  10. #10
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,795

    Re: Character insertion process is very slow

    Quote Originally Posted by VB.NET Developer View Post
    @dday9 see my first post. This thread is not about looping is better via timers etc topic.


    dday asked...
    but you still haven't answered what it is that you do want to do.
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Oct 2018
    Posts
    230

    Re: Character insertion process is very slow

    I want to speed up this process.
    Please dont forget to add good reputation if my advices were useful for you.

  12. #12
    Frenzied Member jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,838

    Re: Character insertion process is very slow

    Speed up what process? That code you have in post #1 does not seem to be very useful.

    What data is initially in TextBox4?
    What data do you want to have in TextBox4 when you are done processing?
    What data do you want in TextBox_A through TextBox_Z?

    It looks like this is some sort of simple encoding system, but we are just guessing.

  13. #13
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,795

    Re: Character insertion process is very slow

    Quote Originally Posted by VB.NET Developer View Post
    I want to speed up this process.
    if you want to speed up the process, then answer the question(s) you were asked
    if you want to speed up the process, then don't use a Timer
    if you want to speed up the process, ......

    sorry I'm in a loop and can't stop my Timer
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Oct 2018
    Posts
    230

    Re: Character insertion process is very slow

    Speed up what process? That code you have in post #1 does not seem to be very useful.
    I have inserted words in textbox4 with fixed length such as - "like have more tech nome fren zzer lake like reac brod chue wang"

    What data is initially in TextBox4?
    Extracted words with fixed length from many international books. It works fast for small amount of words, but once I have a lot of (more than 10 000), inserting process is painfully slow... and significantly slower as more text is processed.

    What data do you want to have in TextBox4 when you are done processing?
    The same except instead of space there will be predefined characters to replace space such as: - "likeAhaveAmoreAtechBnomeBfrenBzzerAlakeAlikeAreacBbrodBchueBwang"

    What data do you want in TextBox_A through TextBox_Z?
    TextBoxA means textbox1, TextBoxB means textbox2, but I have used different names rather than default ones.
    Please dont forget to add good reputation if my advices were useful for you.

  15. #15
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    10,252

    Re: Character insertion process is very slow

    Strings are immutable in Visual Basic .NET.

    This means that every time your timer ticks and you start to manipulate the String, it is literally destroying the old instance and creating a new one.

    Most of the time, this is fine, but doing this at an interval on 10k+ words, you will get a performance hit. To be honest, I'm genuinely surprised you haven't hit an OutOfMemory exception yet.

    I could deduce from your code what it is was that it was doing, but the context behind it is still unclear.

  16. #16
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,294

    Re: Character insertion process is very slow

    What you're doing sounds rather odd, but regardless, if you want to speed up the string processing then you should use an object that is made for faster string processing i.e. a string builder and maintain your string there.
    Only use the textbox for displaying the results, don't use the textbox for storing and modification.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Oct 2018
    Posts
    230

    Re: Character insertion process is very slow

    Quote Originally Posted by passel View Post
    Only use the textbox for displaying the results, don't use the textbox for storing and modification.
    Heres my updated code:

    Code:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim StrData As String = TextBox4.Text
            Dim sb1 As New System.Text.StringBuilder
            Dim strdata3 As String = StrData
            StrData = Replace(StrData, " ", "A", , TextBox_A.Text)
            StrData = Replace(StrData, " ", "B", , TextBox_B.Text)
            sb1.Append(strdata3.ToString)
            TextBox5.Text = strdata3.ToString
    End Sub
    Actually, I am now using two textboxes, one for word extraction and one for processing, but nothing happens (only original text is copied) when I click on the button that starts the timer. I am using another textbox for displaying the result and I am using strdata3 as a preprocessor.
    Please dont forget to add good reputation if my advices were useful for you.

  18. #18
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,557

    Re: Character insertion process is very slow

    Quote Originally Posted by VB.NET Developer View Post
    Heres my updated code:

    Code:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim StrData As String = TextBox4.Text
            Dim sb1 As New System.Text.StringBuilder
            Dim strdata3 As String = StrData
            StrData = Replace(StrData, " ", "A", , TextBox_A.Text)
            StrData = Replace(StrData, " ", "B", , TextBox_B.Text)
            sb1.Append(strdata3.ToString)
            TextBox5.Text = strdata3.ToString
    End Sub
    Actually, I am now using two textboxes, one for word extraction and one for processing, but nothing happens (only original text is copied) when I click on the button that starts the timer. I am using another textbox for displaying the result and I am using strdata3 as a preprocessor.
    Stop using a timer for this, it is pure insanity. This is obviously related to a previous thread of yours where you were looking to replace characters in a pattern, like replace the first 8 spaces in a string with A, the next 3 spaces in that string with B, and so on. Using a timer for this guarantees it will be slow because it is going to take (Timer.Interval * (Number of spaces in your massive amount of text) / (TextBox_A.Text + TextBox_B.Text)) milliseconds to complete no matter how optimized the actual code inside the timer tick event is.

    It's like being mad that it takes someone 1 hour to write down 60 words. But it turns out you are only giving them 1 word per minute, it takes them 3 seconds to write each word down, and then they are sitting idle for 57 seconds. Not because they are lazy or slow, but because your process of only giving them 1 word per minute is the bottleneck.

    Also, your variable usage is frightening. I would be curious to hear your explanation on what you think that StringBuilder object is accomplishing for you.

    Debug your code. The reason that the output is the same as the input is because the output is coming straight from a variable that came straight from the (unchanged) input.

    Good luck.

  19. #19
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,795

    Re: Character insertion process is very slow

    here a real simple replace

    Code:
     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            TextBox1.Text = "welcome to vbForums, don't use a Timer if you don't have to"
        End Sub
    
        Private Function replaceInString(ByVal source As String) As String
            source = source.Replace("o"c, "9").Replace("a"c, "\").Replace("b"c, "*").Replace("h"c, "(").Replace(" ", "X")
            Return source
        End Function
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            TextBox2.Text = replaceInString(TextBox1.Text)
        End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

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