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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Loading data to string variable

Loading data to string variable

Scheduled Pinned Locked Moved C#
performanceasp-nethelpquestion
4 Posts 3 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.
  • R Offline
    R Offline
    Rostrox
    wrote on last edited by
    #1

    I am trying to load data to an string variable in the next way: . . . string s = ""; string text = ""; do { If ( text have some conditions) s = s + text + "\n"; text = reader.ReadLine( ); } while (text != null); . . Then I need to show string "s" to user using a multiline textbox created with Web Forms. The problem is that the previous loop lasts many, many minutes. It tries to load 2MB but in a slow manner, I imagine that the memory allocation is the problem. I need textbox for copy/paste purposes. Is there anyway to enhance the speed of s = s + text + "\n"; ??? Best regards.

    C 1 Reply Last reply
    0
    • R Rostrox

      I am trying to load data to an string variable in the next way: . . . string s = ""; string text = ""; do { If ( text have some conditions) s = s + text + "\n"; text = reader.ReadLine( ); } while (text != null); . . Then I need to show string "s" to user using a multiline textbox created with Web Forms. The problem is that the previous loop lasts many, many minutes. It tries to load 2MB but in a slow manner, I imagine that the memory allocation is the problem. I need textbox for copy/paste purposes. Is there anyway to enhance the speed of s = s + text + "\n"; ??? Best regards.

      C Offline
      C Offline
      Corinna John
      wrote on last edited by
      #2

      System.Text.StringBuilder is a little faster: StringBuilder s = new StringBuilder(); string text = ""; do { If ( text have some conditions) s.AppendFormat("{1}\n", text); text = reader.ReadLine( ); } while (text != null);

      J 2 Replies Last reply
      0
      • C Corinna John

        System.Text.StringBuilder is a little faster: StringBuilder s = new StringBuilder(); string text = ""; do { If ( text have some conditions) s.AppendFormat("{1}\n", text); text = reader.ReadLine( ); } while (text != null);

        J Offline
        J Offline
        Jeff Varszegi
        wrote on last edited by
        #3

        Corinna, Wouldn't this be even a little faster?

        StringBuilder s = new StringBuilder();
        string text; // No need to assign the variable to a string that's then thrown away
        while ((text = reader.readLine()) != null) {
        if ( <condition> )
        s.Append(text).Append('\n');
        }

        Regards, Jeff Varszegi [edit] Forgot to include the condition! I'm gonna test the impact of AppendFormat() versus the extra method call. JKV [/edit]

        1 Reply Last reply
        0
        • C Corinna John

          System.Text.StringBuilder is a little faster: StringBuilder s = new StringBuilder(); string text = ""; do { If ( text have some conditions) s.AppendFormat("{1}\n", text); text = reader.ReadLine( ); } while (text != null);

          J Offline
          J Offline
          Jeff Varszegi
          wrote on last edited by
          #4

          I was interested enough to run a performance test because I never used AppendFormat before, and I know that StringBuilder is highly optimized. I ran this code in Debug mode in VS .NET 2003 on my laptop:

          StringBuilder sb;
          string text = "abc";
          int x, y;
          long startTime, endTime;

          startTime = DateTime.Now.Ticks;
          for(y = 0; y < 5000; y++) {
          sb = new StringBuilder(10000);
          for (x = 0; x < 250; x++) {
          sb.Append(text).Append('\n');
          sb.Append(text).Append('\n');
          sb.Append(text).Append('\n');
          sb.Append(text).Append('\n');
          sb.Append(text).Append('\n');
          sb.Append(text).Append('\n');
          sb.Append(text).Append('\n');
          sb.Append(text).Append('\n');
          sb.Append(text).Append('\n');
          sb.Append(text).Append('\n');
          }
          }
          endTime = DateTime.Now.Ticks;
          Console.WriteLine(((endTime - startTime) / 10000) + " ms using Append()");

          startTime = DateTime.Now.Ticks;
          for(y = 0; y < 5000; y++) {
          sb = new StringBuilder(10000);
          for (x = 0; x < 250; x++) {
          sb.AppendFormat("{0}\n", text);
          sb.AppendFormat("{0}\n", text);
          sb.AppendFormat("{0}\n", text);
          sb.AppendFormat("{0}\n", text);
          sb.AppendFormat("{0}\n", text);
          sb.AppendFormat("{0}\n", text);
          sb.AppendFormat("{0}\n", text);
          sb.AppendFormat("{0}\n", text);
          sb.AppendFormat("{0}\n", text);
          sb.AppendFormat("{0}\n", text);
          }
          }
          endTime = DateTime.Now.Ticks;
          Console.WriteLine(((endTime - startTime) / 10000) + " ms using AppendFormat()");

          It printed these results: 1682 ms using Append() 7801 ms using AppendFormat() Interestingly enough, creating a separate variable to hold the format string and using that in calls to AppendFormat increased the run time of the second section a little. I still know next to nothing about the IL, but I'm sure there's some good explanation for this. Regards, Jeff Varszegi

          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