Search code examples
c#performancevariablesdeclarationcpu-speed

When to declare variables in C# regarding efficiency/speed


I am running an implementation of something, fully built for efficiency. I am not that experienced on this topic yet, and would like to know when to declare variables best. The following part of my code particularly:

//Variables not declared in the next part are declared here (like xx, y1, x1.....)
    for(s = 0; s < this.Width; s++)
        {
            y = ymin;
            for(z = 0; z < this.Height; z++)
            {
                x1 = 0;
                y1 = 0;
                looper = 0;
                while(looper < curMaxIter && Math.Sqrt((x1 * x1) + (y1 * y1)) < 2)
                {
                    looper++;
                    xx = (x1 * x1) - (y1 * y1) + x;
                    y1 = 2 * x1 * y1 + y;
                    x1 = xx;
                }
                double perc = looper / (double)curMaxIter;
                int val = ((int)(perc * 255));
                b.SetPixel(s,z,cs[val]);
                y += intigralY;
            }
            x += intigralX;
        }

As you all can imagine, this while loop continues for a while... and i want to reduce the time it takes as much as possible in any way, while the result will be the same. This whole part of code is again run for thousands of times per frame (I'm rendering Mandelbrot images for those curious). My main question: is it faster to declare variables (like perc and val, but also like xx, y1 and x1) right before i use them? (like perc and val) or better declare them before the whole loops? (Like i did with xx, y1 and x1 etc.)


Solution

  • I recommend reading Compilers - What Every Programmer Should Know About Compiler Optimization. As a result, what micro-optimizations you're attempting will most likely have zero effect. The compiler and JIT team is extremely smart. For example:

    What’s the difference between RyuJIT and Visual C++ in terms of optimization capabilities? Because it does its work at run time, RyuJIT can perform optimizations that Visual C++ can’t. For example, at run time, RyuJIT might be able to determine that the condition of an if statement is never true in this particular run of the application and, therefore, it can be optimized away.

    Performance example, which one is faster?:

    List<User> list = new List<User>();
    User u;
    
    foreach (string s in l)
    {
        u = new User();
        u.Name = s;
        list.Add(u);
    }
    

    Or this one:

    List<User> list = new List<User>();
    
    foreach (string s in l)
    {
        User u = new User();
        u.Name = s;
        list.Add(u);
    }
    

    Answer:

    Performance-wise both examples are compiled to the same IL, so there's no difference.

    So the only way to tell is to run the tests themselves... that is if you Absolutely need that speed.

    Other good articles:

    The Sad Tragedy of Micro-Optimization Theater

    Hardware is Cheap, Programmers are Expensive