Results 1 to 3 of 3

Thread: [RESOLVED] [2.0] repetitive string modification slowing down application...

  1. #1

    Thread Starter
    Addicted Member effekt26's Avatar
    Join Date
    Nov 2006
    Posts
    138

    Resolved [RESOLVED] [2.0] repetitive string modification slowing down application...

    Ok, so im running a loop, and every iteration of this loop adds some text to an "output" textbox, which can be later saved as a log file...

    Ok, say i have the following code...

    Code:
    for (int i = 0; i < 1000; i++) {
    output.Text = output.Text + "some big long string here, much longer than this text...\r\n";
    }
    as i increases, it takes marginally longer each iteration to add the text... in fact, the time it takes is exponential...which makes sense..

    what im wondering is, is there like an Append() method, or something similar...or how would i go about creating one, as this is a really slow part of my app. and i just know that it can be coded much more efficently, i just havent nutted out how.


    thanks for the help in advance...
    Cheers,
    Justin

  2. #2
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: [2.0] repetitive string modification slowing down application...

    String is an immutable object and everytime you concatenate or append something, a new memory is allocated, slowing down the process.

    To append large data togther, you must use object of System.Text.StringBuilder class.

    Code:
    System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
    for (int i = 0; i < 1000; i++) {
    stringBuilder.Append("some big long string here, much longer than this text...\r\n");
    }
    //display here using
    Console.WriteLine(stringBuilder.ToString());
    Show Appreciation. Rate Posts.

  3. #3

    Thread Starter
    Addicted Member effekt26's Avatar
    Join Date
    Nov 2006
    Posts
    138

    Re: [2.0] repetitive string modification slowing down application...

    Quote Originally Posted by Harsh Gupta
    String is an immutable object and everytime you concatenate or append something, a new memory is allocated, slowing down the process.

    To append large data togther, you must use object of System.Text.StringBuilder class.

    Code:
    System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
    for (int i = 0; i < 1000; i++) {
    stringBuilder.Append("some big long string here, much longer than this text...\r\n");
    }
    //display here using
    Console.WriteLine(stringBuilder.ToString());
    Hi, and thanks for the reply, that is exactly what i was looking for..

    Cheers, and thanks again...
    Justin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width