setting large amount of text in textbox
-
The textbox aren't really designed for holding 20MB of text. Try opening the file in notepad and see how much time it take. A RichTextBox might be a better choice in your case.
.jpg wrote:
The textbox aren't really designed for holding 20MB of text
He he, don't tell me :-). the rich textbox does give a slight improvement :-).
V.
Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive -
I'm not too sure about putting text into a textbox quicker, but if you definately know that every 2nd character is a letter, then using
text[i] = (char)((int)text[i] - 32);
is quicker than usingtext[i] = char.ToUpper(text[i]);
if(char.IsLetter(text[i])){
text[i] = (char)((int)text[i] - 32); //char.ToUpper(text[i]);
}gained me 1 second thanks :-) (this is actually kinda fun ;-), I learned a lot here... ;P)
V.
Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive -
if(char.IsLetter(text[i])){
text[i] = (char)((int)text[i] - 32); //char.ToUpper(text[i]);
}gained me 1 second thanks :-) (this is actually kinda fun ;-), I learned a lot here... ;P)
V.
Stop smoking so you can: Enjoy longer the money you save. Moviereview ArchiveYou could even call your progress bar update fewer times than you are, with a 20 meg file you're currently calling the progress bar update more than 20,000 times. If this takes 10 secs to go through the for loop, that's 2000 progress bar updates per second, bit of overkill there.
-
Guys, A few days ago I asked about an assignment we had to do here at work. convert "aabb" to "aAbB" and set it to the textbox as quickly as possible. I think the conversion itself is very quick, reading in and converting in 1 or 2 seconds, but setting the entire thing in the texbox takes me another 10 seconds! (20 MB file) I've read about the AppendText property and tried to set my characterarray to the box inside my loop, but so far the fastest results I got where form
txtbox_result.Text = new string(text);
where text = char []. Any ideas on how to speed this up? (Suspend/resumelayout don't help either) thanks ! PS: here's tho code I got so far:starttime = DateTime.Now; reader = new System.IO.StreamReader(openfiledlg.FileName); char \[\] text = reader.ReadToEnd().ToCharArray(); reader.Close(); difference = DateTime.Now - starttime; lbl\_result.Text = "File read: " + difference.TotalMilliseconds + " milliseconds."; lbl\_result.Refresh(); pb\_conversion.Maximum = text.Length; //Starting from 1 will gain me 1 character less to analyze. //don't update the progress bar every iteration. int i2 = 0; for(int i = 1; i < text.Length; i+=2){ text\[i\] = char.ToUpper(text\[i\]); if(++i2 == 1000){ i2 = 0; pb\_conversion.Value = i; } //end if } //end for difference = DateTime.Now - starttime; lbl\_result.Text = "conversion: " + difference.TotalMilliseconds + " milliseconds."; lbl\_result.Refresh(); pb\_conversion.Value = pb\_conversion.Maximum; txtbox\_result.SuspendLayout(); txtbox\_result.Text = new string(text); txtbox\_result.ResumeLayout(); difference = DateTime.Now - starttime; lbl\_result.Text = "Done in: " + difference.TotalMilliseconds + " milliseconds.";
V. I found a living worth working for, but haven't found work worth living for.
-
Instead of using
txtbox_result.Text = new string(text);
Do thistxtbox_result.AppendText(new string(text));
tnx, good idea, but didn't help. :)
V.
Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive -
tnx, good idea, but didn't help. :)
V.
Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive -
mine is, with ngen and Highest priority thread about 16 seconds. Maybe you have more memory and faster cpu? (Centrino 2,13GHz, 2Gb memory)
V.
Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive -
mine is, with ngen and Highest priority thread about 16 seconds. Maybe you have more memory and faster cpu? (Centrino 2,13GHz, 2Gb memory)
V.
Stop smoking so you can: Enjoy longer the money you save. Moviereview ArchiveHere's my code. I'm using a standard textbox.
textBox2.Clear(); DateTime dt = DateTime.Now; StreamReader sr = new StreamReader(@"C:\test\test.txt"); char[] c = sr.ReadToEnd().ToCharArray(); progressBar1.Maximum = c.Length; for (int i = 0; i < c.Length; i += 2) { if ((int)c[i] >= 97 && (int)c[i] <= 122) c[i] = (char)((int)c[i] - 32); if (i % 10000 == 0) progressBar1.Value = i; } textBox2.AppendText(new string(c)); MessageBox.Show((DateTime.Now.Ticks - dt.Ticks).ToString());
Note that a textbox does only store 32k of text, but if your assignment says you just have to fill a textbox, then you are only doing what it says...
-
Here's my code. I'm using a standard textbox.
textBox2.Clear(); DateTime dt = DateTime.Now; StreamReader sr = new StreamReader(@"C:\test\test.txt"); char[] c = sr.ReadToEnd().ToCharArray(); progressBar1.Maximum = c.Length; for (int i = 0; i < c.Length; i += 2) { if ((int)c[i] >= 97 && (int)c[i] <= 122) c[i] = (char)((int)c[i] - 32); if (i % 10000 == 0) progressBar1.Value = i; } textBox2.AppendText(new string(c)); MessageBox.Show((DateTime.Now.Ticks - dt.Ticks).ToString());
Note that a textbox does only store 32k of text, but if your assignment says you just have to fill a textbox, then you are only doing what it says...
thanks, i'll try this tomorrow. Last day for submitting our tries :-). Maybe I'll post in the lounge next week how I did (or we did ;P)
V.
Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive -
I'm not too sure about putting text into a textbox quicker, but if you definately know that every 2nd character is a letter, then using
text[i] = (char)((int)text[i] - 32);
is quicker than usingtext[i] = char.ToUpper(text[i]);
Unfortunately this will only work for ANSI characters, so you're actually introducing a bug in order to gain a few ms... Not a good deal IMHO.
Regards, mav -- Black holes are the places where God divided by 0...
-
I'm not too sure about putting text into a textbox quicker, but if you definately know that every 2nd character is a letter, then using
text[i] = (char)((int)text[i] - 32);
is quicker than usingtext[i] = char.ToUpper(text[i]);
I am not surprised it is faster as ToUpper actually converts to upper case. :) Subtracting 32 works in a limited subset of letters (the A-Z range and a few others), and it ignores cultural settings. Sure it might be fine enough for a "just for the fun of it" project like this, but unfortunately people end up beleiving they actually can convert using code like this in real projects as well. :(
-
Unfortunately this will only work for ANSI characters, so you're actually introducing a bug in order to gain a few ms... Not a good deal IMHO.
Regards, mav -- Black holes are the places where God divided by 0...
-
It's not introducing a bug, if you had read the original thread from a few days ago you'd realise that this is what the OP wanted - 'convert "aabb" to "aAbB" and set it to the textbox as quickly as possible'. Thanks.
Ok, if it's really just A's and B's then it'll work of course. I guess I'm already going on auto when seeing someone performing integer maths on characters :)
Regards, mav -- Black holes are the places where God divided by 0...