Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. speed boost

speed boost

Scheduled Pinned Locked Moved C#
cssperformancequestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    V 0
    wrote on last edited by
    #1

    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.

    A C P 3 Replies Last reply
    0
    • V V 0

      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.

      A Offline
      A Offline
      althamda
      wrote on last edited by
      #2

      You could go through the text as a char array and just subtract 32 from each other char to get uppercase. Might be a little faster.

      1 Reply Last reply
      0
      • V V 0

        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.

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        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 )

        V 1 Reply Last reply
        0
        • C Christian Graus

          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 )

          V Offline
          V Offline
          V 0
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • V V 0

            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.

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            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.

            V A 2 Replies Last reply
            0
            • P Pete OHanlon

              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.

              V Offline
              V Offline
              V 0
              wrote on last edited by
              #6

              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

              P 1 Reply Last reply
              0
              • V V 0

                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

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                Glad to be of service.:-D

                Deja View - the feeling that you've seen this post before.

                1 Reply Last reply
                0
                • P Pete OHanlon

                  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.

                  A Offline
                  A Offline
                  althamda
                  wrote on last edited by
                  #8

                  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); }

                  P 1 Reply Last reply
                  0
                  • A althamda

                    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); }

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups