Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Stringbuilder to improve performance

Options
  • 13-02-2010 12:15pm
    #1
    Closed Accounts Posts: 25


    Hello all

    Lang: C#

    I'm doing the simple CLR profiler tutorial found at http://msdn.microsoft.com/en-us/library/ms979205.aspx

    where it tells me StringBuilder is the solution to ProfilerSample1.cs
    StringBuilder s = new StringBuilder("");
    

    no syntax errors but gives me

    'StringBuilder' could not be found (are you missing a using directive or an assembly reference?)



    //PS:If anyone is aware of the using block solution to ProfilerSample2.cs and could point me in the correct direction. Atm i'm trying a using statement in the constructor where I have defined DrawBrush()


Comments

  • Registered Users Posts: 981 ✭✭✭fasty


    The documentation says StringBuilder is in the assembly mscorlib.dll and in the namespace System.Text.

    So are you missing a using directive? It couldn't be an assembly reference!


  • Closed Accounts Posts: 25 BillMcErlane


    Thanks for the prompt reply

    This is my code:
    using System;
    using System.Text;
    public class ProfilerSample1
    {
        static void Main (String[] args)
        {
            
            int start = Environment.TickCount;
            for (int i = 0; i < 1000; i++)
            {
                StringBuilder s = new StringBuilder("");
                    for (int j = 0; j < 100; j++)
                    {
                        s += "Outer index = ";
                        s += i;
                        s += " Inner index = ";
                        s += j;
                        s += " ";
                    }
            }
            Console.WriteLine("Program ran for {0} seconds", 
       0.001*(Environment.TickCount - start));
        }
    }
    

    The addition of System.Text has thrown up a few more errors for me to deal with. I figure it is just to do with amending the string.

    Thank you for the solution breakdown


  • Registered Users Posts: 4,277 ✭✭✭km991148


    //PS:If anyone is aware of the using block solution to ProfilerSample2.cs and could point me in the correct direction. Atm i'm trying a using statement in the constructor where I have defined DrawBrush()

    Well the problem there is that a Brush (which SolidBrush is derived from) object is being created in each loop iteration. This means many brushes are left on the heap (an area of memory) awaiting garbage collection. Brush is Idisposbale (as alluded to in the //brush has a finalizer comment) which means that dispose should be called on this when finished to alert the runtime that it can be cleaned up and all resources are to be released;

    The best way to do this is as you say with a using statement; If you can use only one brush do:
    using(Brush b = new SolidBrush(Color.Black)){
    // the code
    // the loop
    //etc


    }


    if you need to use many brushes for whatever reason do:
    //forloop{
    using(Brush b = new SolidBrush(Color.Black)){
    // the code
    //etc
    }
    //}

    Remember using is just a shortcut for:
    try{
    // declare disposable obj d
    }
    finally{
    d.Dispode;
    }


  • Registered Users Posts: 4,277 ✭✭✭km991148


    Thanks for the prompt reply

    This is my code:
    using System;
    using System.Text;
    public class ProfilerSample1
    {
        static void Main (String[] args)
        {
            
            int start = Environment.TickCount;
            for (int i = 0; i < 1000; i++)
            {
                StringBuilder s = new StringBuilder("");
                    for (int j = 0; j < 100; j++)
                    {
                        s += "Outer index = ";
                        s += i;
                        s += " Inner index = ";
                        s += j;
                        s += " ";
                    }
            }
            Console.WriteLine("Program ran for {0} seconds", 
       0.001*(Environment.TickCount - start));
        }
    }
    

    The addition of System.Text has thrown up a few more errors for me to deal with. I figure it is just to do with amending the string.

    Thank you for the solution breakdown

    I dont think its the addition of the using statment, its more to do with your usage of stringbuilder.. it has a bunch of methods for appenting text like:

    s.Append(...
    S.AppendFormat("", x, y, x);
    s.AppendLine(....

    doing string += another string is what the string builder is trying to stop;
    i.e. with
    string x = "123";
    string y = "456";
    the line
    y += x;
    requires memory to be allocated for x, y and another block of memory the lenghth of x and y,
    whereas think of string builder as a collection of pointers to each existing string, the string its self isnt recreated each time you do string.append, only at the end when you do stringbuilder s; s.ToString() is when the full string length is required in mem.


Advertisement