-
Mar 16th, 2021, 01:19 PM
#1
Thread Starter
Hyperactive Member
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.
How? Under this post there is "RATE THIS POST" button. Click on it.
-
Mar 16th, 2021, 01:37 PM
#2
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.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Mar 16th, 2021, 03:24 PM
#3
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.
-
Mar 16th, 2021, 03:40 PM
#4
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.
-
Mar 16th, 2021, 03:50 PM
#5
Thread Starter
Hyperactive Member
Re: Character insertion process is very slow
Originally Posted by ChrisE
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.
How? Under this post there is "RATE THIS POST" button. Click on it.
-
Mar 16th, 2021, 03:54 PM
#6
Re: Character insertion process is very slow
Originally Posted by VB.NET Developer
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.
-
Mar 16th, 2021, 03:57 PM
#7
Thread Starter
Hyperactive Member
Re: Character insertion process is very slow
Originally Posted by OptionBase1
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.
How? Under this post there is "RATE THIS POST" button. Click on it.
-
Mar 16th, 2021, 04:18 PM
#8
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.
-
Mar 16th, 2021, 04:21 PM
#9
Thread Starter
Hyperactive Member
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.
How? Under this post there is "RATE THIS POST" button. Click on it.
-
Mar 16th, 2021, 04:35 PM
#10
Re: Character insertion process is very slow
Originally Posted by VB.NET Developer
@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.
-
Mar 16th, 2021, 04:50 PM
#11
Thread Starter
Hyperactive Member
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.
How? Under this post there is "RATE THIS POST" button. Click on it.
-
Mar 16th, 2021, 04:58 PM
#12
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.
-
Mar 16th, 2021, 05:02 PM
#13
Re: Character insertion process is very slow
Originally Posted by VB.NET Developer
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.
-
Mar 16th, 2021, 05:16 PM
#14
Thread Starter
Hyperactive Member
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.
How? Under this post there is "RATE THIS POST" button. Click on it.
-
Mar 16th, 2021, 06:12 PM
#15
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.
-
Mar 17th, 2021, 08:07 AM
#16
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
-
Mar 21st, 2021, 01:19 PM
#17
Thread Starter
Hyperactive Member
Re: Character insertion process is very slow
Originally Posted by passel
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.
How? Under this post there is "RATE THIS POST" button. Click on it.
-
Mar 21st, 2021, 01:56 PM
#18
Re: Character insertion process is very slow
Originally Posted by VB.NET Developer
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.
-
Mar 22nd, 2021, 12:51 AM
#19
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|