Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static string vs non-static string

I'm a game programmer, and so I'm always trying to optimize my code. I was playing around with loops lately, and I came across something that confused me a little bit. I created a simple console application and created a non-static class:

public class CL
{
    static int X;
    string Z;
    static string sZ;

    public CL(int x, int y, string z)
    {
        X = x + 1;
        Z = z;
        sZ = z;
    }

    public void Update(int i)
    {
        X *= i + 1;
        X = X / 2;
        Z += i.ToString() ;
    }

    public void UpdateStatic(int i)
    {
        X *= i + 1;
        X = X / 2;
        sZ += i.ToString();
    }
}

There are two methods that both perform the same operations: they both modify the static integer X, and then they add the integer i onto the string that is either static (sZ in UpdateStatic) or not (Z in Update).

I ran a loop, and here are the results: enter image description here

As you can see, modifying the static string variable took about 260 times as long as modifying the non-static (which could kill a game's fps). I tried making the integer X both static and not, but there was no performance difference. So my question is: Why does modifying the static string hurt performance?, but modifying a static integer not hurt? Here's the rest of the code:

    static void Main(string[] args)
    {
        while (Console.ReadLine() != "Q")
        {
            int count = 20000;
            List<CL> l = new List<CL>();
            List<CL> sl = new List<CL>();
            for (int i = 0; i < count; i++)
            {
                var cl = new CL(i, i * 2, "");
                l.Add(cl);
                sl.Add(cl);
            }
            Stopwatch s = new Stopwatch();
            s.Start();
            for (int i = 0; i < count; i++)
            {
                l[i].Update(i);
            }
            s.Stop();
            Console.WriteLine(s.Elapsed + " unsorted list modifying non-static variable");
            s.Reset();

            s.Start();
            for (int i = 0; i < count; i++)
            {
                sl[i].UpdateStatic(i);
            }
            s.Stop();
            Console.WriteLine(s.Elapsed + " unsorted list modifying static variable");
            s.Reset();
        }

    }

I couldn't find anything about why this would happen. Sorry if the answer's obvious. Thanks for your help.

like image 749
davidsbro Avatar asked Dec 25 '22 13:12

davidsbro


1 Answers

In the non-static version, you are using a different instance of CL in every iteration of the loop. Therefore Z never gets very long.

In the static version, sZ continues to get longer and longer.

Creating a string and appending has runtime proportional to its length, so the static version takes much longer as a result.

As for the integer, there is no difference in runtime due to the value of an integer. This is because internally, all integers are the same size in .net. (4 bytes)

like image 157
recursive Avatar answered Jan 23 '23 00:01

recursive