-4

i would like to understand if you please help with your opinion on this code below which is the simplest example to the performance issue using .net built-in methods.. and they're highly recommended by online tutorials ...

    public void stringFormatVsConcatShowResults()
    {
        float timeFormat = 0, timeConcat = 0;
                for (int counter = 0; counter < 10; i++)
                {

                    if (counter % 2 == 0)
                         timeFormat += TestStrFormat(1000000, true);
                    else timeConcat += TestStrFormat(1000000);

                }


                if (timeFormat > timeConcat)
                {
                    float precWonBy = (timeConcat / timeFormat) * 100;
                    int prcfractionalPart = new Version(precWonBy.ToString()).Major;
                    MessageBox.Show("The fstest (by " + prcfractionalPart + " %)is Concat");
                }
                else
                {

                    float precWonBy = (timeConcat / timeFormat) * 100;
                    int precfractionalPart = new Version(precWonBy.ToString()).Major;
                    MessageBox.Show("The fstest (by" + precfractionalPart + " %)is string.Format()");
                }
        }


    public int TestStrFormat(int iterate, bool WstringFormat=false)
    {
        string testStr = string.Empty, Enewline = Environment.NewLine;
        const string header = "its iteration No': ", sign = " !";
        Stopwatch swStrFrm = new Stopwatch();
        swStrFrm.Start();
        if (WstringFormat)
        {

            for (int counter = 0; counter < iterate; i++)
            {
                testStr = string.Format("--{0}{1}{2}--{3}", header, counter, sign, Enewline);
            }

        }
        else
        {
                for (int counter = 0; counter < iterate; i++)
                {
                    testStr = "--its iteration No': " + counter + " !--" + Enewline ;
                }

        }
        swStrFrm.Stop();
        return Convert.ToInt32(swStrFrm.ElapsedTicks);
    }

what would you choose, and have you notest this performance issue ,

this is only a simple example ,

there's lots of methods like capturing screen in to bitmap and converting an Image to byte[]...

as i am new to software development (4 month - self studying) i have encounterd those situations in multiple junctions, where i need to deside , so How do I choose what methods to use?... am i suppose to be a .net renegade when ever i choose to have max performance in my application or it's(C#) intention wasn't performance to begin with.

just Call stringFormatVsConcatShowResults() and see for your self what i'm talking about

  • ReEdit

so with long strings after that said i would do the following :

    public int TestStrWStrBld(int iterate)
    {
        StringBuilder sb = new StringBuilder();

        const string header = "its iteration No': ", sign = " !";

        Stopwatch swStrFrm = new Stopwatch();
        swStrFrm.Start();


            for (int i = 0; i < iterate; i++)
            {
                sb.Append(header).Append(i).Append(sign).Append(Environment.NewLine);
            }


        swStrFrm.Stop();
        return Convert.ToInt32(swStrFrm.ElapsedTicks);
    }

it was now that StringBuilder made it over 600% faster

thanks for opening my mind, from now on ...i'll keep searching for the right implementations.

  • ReEdit2 or for smaller strings : compared to string

Str = "a" + "b" + "c";

the folowing does it even 800% faster !!

    public int TestStrWStrBld(int iterate)
    {
        StringBuilder sb = new StringBuilder();

        const string header = "its iteration No': ", sign = " !";
        string str = string.Empty;
        Stopwatch swStrFrm = new Stopwatch();
        swStrFrm.Start();


            for (int i = 0; i < iterate; i++)
            {
                str= String.Concat(header, i.ToString(), sign,Environment.NewLine);

            }


        swStrFrm.Stop();
        return Convert.ToInt32(swStrFrm.ElapsedTicks);
    }

so i guess the right way is to use String.Concat(string1, string2, etc...)

LoneXcoder
  • 2,121
  • 6
  • 38
  • 76

2 Answers2

3

i have encounterd those situations in multiple junctions, where i need to deside what to go with

I'm guessing your question is "How do I choose what methods to use?"...

While String.Concat is faster in this scenario (about 75% faster on my machine, in an x64 build), in this case, I would suspect that either option will perform adequately. In a real-world scenario, it's unlikely that building a single string like this is going to ever be a performance bottleneck. If you're building large strings, then StringBuilder is likely a better option than either of these...

I would decide what method in the framework to use based on readability and maintainability. Later, when you profile your application and if you find a problem, it's very easy to decide how to adjust this at that point in time.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
1

I'm surprised the tutorials don't tell you to do this :

for (int counter = 0; counter < iterate; i++)
            {
                testStr = new StringBuilder().Append("--its iteration No': ").Append(counter).Append(" !--").Append(Environment.NewLine).toString();
            }
LaGrandMere
  • 10,265
  • 1
  • 33
  • 41
  • Well, it would be slower actually. That said, all of these are almost certainly going to be performing more than fast enough for any realistic performance requirements. – Servy Sep 06 '12 at 16:01