speed boost
-
Hi there, We're having a small test here at work. Read in a text file and convert half the text to uppercase as quickly as possible ("aabb" => "aAbB", so not "AAbb") So I read in the text file and do a for with i+=2 instead of i++ and setting a Stringgbuilder. after the text is done I put text into the text box. Then I use ngen to boost speed a bit (compiler is set to optimized etc.) I also tried to use SuspenLayout and ResumeLayout, but that didn't gain me a lot. Any other tricks I can use to boost this up? (maybe I forgot to set some compiler options or I have to do it in another way? here's my code:
System.IO.StreamReader reader = new System.IO.StreamReader(openfiledlg.FileName); text = new StringBuilder(reader.ReadToEnd()); reader.Close(); pb\_conversion.Maximum = text.Length; starttime = DateTime.Now; //Starting from 1 will gain me 1 character less to analyze. for(int i = 1; i < text.Length; i+=2){ text\[i\] = text\[i\].ToString().ToUpper()\[0\]; pb\_conversion.Value = i; } //end for txtbox\_result.Text = text.ToString(); difference = DateTime.Now - starttime; lbl\_result.Text = "Done in: " + difference.TotalMilliseconds + " milliseconds."; lbl\_result.Visible = true;
I'm curious what else I could do... thanks.
V. I found a living worth working for, but haven't found work worth living for.
-
Hi there, We're having a small test here at work. Read in a text file and convert half the text to uppercase as quickly as possible ("aabb" => "aAbB", so not "AAbb") So I read in the text file and do a for with i+=2 instead of i++ and setting a Stringgbuilder. after the text is done I put text into the text box. Then I use ngen to boost speed a bit (compiler is set to optimized etc.) I also tried to use SuspenLayout and ResumeLayout, but that didn't gain me a lot. Any other tricks I can use to boost this up? (maybe I forgot to set some compiler options or I have to do it in another way? here's my code:
System.IO.StreamReader reader = new System.IO.StreamReader(openfiledlg.FileName); text = new StringBuilder(reader.ReadToEnd()); reader.Close(); pb\_conversion.Maximum = text.Length; starttime = DateTime.Now; //Starting from 1 will gain me 1 character less to analyze. for(int i = 1; i < text.Length; i+=2){ text\[i\] = text\[i\].ToString().ToUpper()\[0\]; pb\_conversion.Value = i; } //end for txtbox\_result.Text = text.ToString(); difference = DateTime.Now - starttime; lbl\_result.Text = "Done in: " + difference.TotalMilliseconds + " milliseconds."; lbl\_result.Visible = true;
I'm curious what else I could do... thanks.
V. I found a living worth working for, but haven't found work worth living for.
-
Hi there, We're having a small test here at work. Read in a text file and convert half the text to uppercase as quickly as possible ("aabb" => "aAbB", so not "AAbb") So I read in the text file and do a for with i+=2 instead of i++ and setting a Stringgbuilder. after the text is done I put text into the text box. Then I use ngen to boost speed a bit (compiler is set to optimized etc.) I also tried to use SuspenLayout and ResumeLayout, but that didn't gain me a lot. Any other tricks I can use to boost this up? (maybe I forgot to set some compiler options or I have to do it in another way? here's my code:
System.IO.StreamReader reader = new System.IO.StreamReader(openfiledlg.FileName); text = new StringBuilder(reader.ReadToEnd()); reader.Close(); pb\_conversion.Maximum = text.Length; starttime = DateTime.Now; //Starting from 1 will gain me 1 character less to analyze. for(int i = 1; i < text.Length; i+=2){ text\[i\] = text\[i\].ToString().ToUpper()\[0\]; pb\_conversion.Value = i; } //end for txtbox\_result.Text = text.ToString(); difference = DateTime.Now - starttime; lbl\_result.Text = "Done in: " + difference.TotalMilliseconds + " milliseconds."; lbl\_result.Visible = true;
I'm curious what else I could do... thanks.
V. I found a living worth working for, but haven't found work worth living for.
I reckon the ToString is probably expensive. Just deal with chars, instead. You can use Char.IsLower to find out if a char is a lower case letter, or just use Char.ToUpper to convert it.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
I reckon the ToString is probably expensive. Just deal with chars, instead. You can use Char.IsLower to find out if a char is a lower case letter, or just use Char.ToUpper to convert it.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
thanks for the reply. I'm around 81 seconds with a 20 Mb file. (84 first)
V.
Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive -
Hi there, We're having a small test here at work. Read in a text file and convert half the text to uppercase as quickly as possible ("aabb" => "aAbB", so not "AAbb") So I read in the text file and do a for with i+=2 instead of i++ and setting a Stringgbuilder. after the text is done I put text into the text box. Then I use ngen to boost speed a bit (compiler is set to optimized etc.) I also tried to use SuspenLayout and ResumeLayout, but that didn't gain me a lot. Any other tricks I can use to boost this up? (maybe I forgot to set some compiler options or I have to do it in another way? here's my code:
System.IO.StreamReader reader = new System.IO.StreamReader(openfiledlg.FileName); text = new StringBuilder(reader.ReadToEnd()); reader.Close(); pb\_conversion.Maximum = text.Length; starttime = DateTime.Now; //Starting from 1 will gain me 1 character less to analyze. for(int i = 1; i < text.Length; i+=2){ text\[i\] = text\[i\].ToString().ToUpper()\[0\]; pb\_conversion.Value = i; } //end for txtbox\_result.Text = text.ToString(); difference = DateTime.Now - starttime; lbl\_result.Text = "Done in: " + difference.TotalMilliseconds + " milliseconds."; lbl\_result.Visible = true;
I'm curious what else I could do... thanks.
V. I found a living worth working for, but haven't found work worth living for.
I had a play round with this, and got a 21 MB file to process in about 10 seconds. This code isn't perfect, but it is a decent basis to carry on with:
public void ReadFile() { if (openfiledialog.ShowDialog() == DialogResult.OK) { System.IO.StreamReader reader = new System.IO.StreamReader(openfiledialog.FileName); StringBuilder text1 = new StringBuilder(reader.ReadToEnd()); reader.Close(); pb_conversion.Maximum = text1.Length; DateTime starttime = DateTime.Now; //Starting from 1 will gain me 1 character less to analyze. int innercount = 0; for(int i = 1; i < text1.Length; i+=2) { text1[i] = text1[i].ToString().ToUpper()[0]; if (++innercount == 1000) { innercount = 0; pb_conversion.Value = i; } } txtbox_result.Text = text1.ToString(); TimeSpan difference = DateTime.Now - starttime; MessageBox.Show(difference.ToString()); } }
One of the key areas is that you no longer attempt to update the progress bar every iteration. This is just wasteful and should be avoided - which is why I update every 1000 iterations.
Deja View - the feeling that you've seen this post before.
-
I had a play round with this, and got a 21 MB file to process in about 10 seconds. This code isn't perfect, but it is a decent basis to carry on with:
public void ReadFile() { if (openfiledialog.ShowDialog() == DialogResult.OK) { System.IO.StreamReader reader = new System.IO.StreamReader(openfiledialog.FileName); StringBuilder text1 = new StringBuilder(reader.ReadToEnd()); reader.Close(); pb_conversion.Maximum = text1.Length; DateTime starttime = DateTime.Now; //Starting from 1 will gain me 1 character less to analyze. int innercount = 0; for(int i = 1; i < text1.Length; i+=2) { text1[i] = text1[i].ToString().ToUpper()[0]; if (++innercount == 1000) { innercount = 0; pb_conversion.Value = i; } } txtbox_result.Text = text1.ToString(); TimeSpan difference = DateTime.Now - starttime; MessageBox.Show(difference.ToString()); } }
One of the key areas is that you no longer attempt to update the progress bar every iteration. This is just wasteful and should be avoided - which is why I update every 1000 iterations.
Deja View - the feeling that you've seen this post before.
dude, I couldn't believe my eyes when I saw the difference :omg: 11 seconds ! thanks man.
V.
Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive -
dude, I couldn't believe my eyes when I saw the difference :omg: 11 seconds ! thanks man.
V.
Stop smoking so you can: Enjoy longer the money you save. Moviereview ArchiveGlad to be of service.:-D
Deja View - the feeling that you've seen this post before.
-
I had a play round with this, and got a 21 MB file to process in about 10 seconds. This code isn't perfect, but it is a decent basis to carry on with:
public void ReadFile() { if (openfiledialog.ShowDialog() == DialogResult.OK) { System.IO.StreamReader reader = new System.IO.StreamReader(openfiledialog.FileName); StringBuilder text1 = new StringBuilder(reader.ReadToEnd()); reader.Close(); pb_conversion.Maximum = text1.Length; DateTime starttime = DateTime.Now; //Starting from 1 will gain me 1 character less to analyze. int innercount = 0; for(int i = 1; i < text1.Length; i+=2) { text1[i] = text1[i].ToString().ToUpper()[0]; if (++innercount == 1000) { innercount = 0; pb_conversion.Value = i; } } txtbox_result.Text = text1.ToString(); TimeSpan difference = DateTime.Now - starttime; MessageBox.Show(difference.ToString()); } }
One of the key areas is that you no longer attempt to update the progress bar every iteration. This is just wasteful and should be avoided - which is why I update every 1000 iterations.
Deja View - the feeling that you've seen this post before.
-
Wouldn't using a char[] be faster than using a StringBuilder?
char[] c = sr.ReadToEnd().ToCharArray(); for (int i = 0; i < c.Length; i+=2) { c[i] = (char)((int)c[i] - 32); }
It would, but you need to enclose the c[i] test with an if (char.IsLetter(c[i])... so that you don't end up converting none alphabetic characters.
Deja View - the feeling that you've seen this post before.