-2

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.)

Merlijn
  • 81
  • 2
  • 9
  • 3
    If you were curious what was faster, why don't you try it out and see what effect it has, if any? – mason Jul 09 '18 at 20:00
  • 3
    Have you benchmarked it? Compilers are really good at optimizing such things, your assumptions may most likely turn out to be wrong – UnholySheep Jul 09 '18 at 20:00
  • 4
    You're assuming it even matters. It doesn't, the JITter tracks where variables are used (the range of instructions) and optimize usage based on this, such as placing variables that are only used in a localized area into registers, etc. If you would like to improve this code regarding variables my only suggestion would be to use proper names, `xx`, `x1`, `y1`, `b`, `looper`, etc. Use descriptive names and know that variable name length has no effect on performance whatsoever. If you absolutely want to increase the performance of this code, start with a profiler and find the bottleneck that way. – Lasse V. Karlsen Jul 09 '18 at 20:02
  • Likely your execution speed here is limited by `Math.Sqrt` as this tends to be a long-latency operation. As others have said a profiler likely would reveal this, though you might need a very low-level profiler that can show you CPU level stalls. Looks like you could just square both sides of that inequality and avoid the square root all together. – Andy Ayers Jul 13 '18 at 05:49

2 Answers2

5

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

Joe
  • 41,484
  • 20
  • 104
  • 125
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • Thank you for the reply! I will be reading that for sure! Indeed i assumed it matters, good to know it barely does! – Merlijn Jul 09 '18 at 20:13
-1

You can use Visual Studio Performance Profiler https://msdn.microsoft.com/en-us/library/ms182372.aspx

Sergei Zinovyev
  • 1,238
  • 14
  • 14
  • Please take a moment and read [Are answers that just contain links elsewhere really "good answers"?](https://meta.stackexchange.com/a/8259/171858). When someone goes on Stack Overflow, the question "answer" should actually contain an answer. Not just a bunch of directions towards the answer. – Erik Philips Jul 09 '18 at 20:51
  • 1
    Cannot agree with it. If you read the question, you will see that it needs direction. – Sergei Zinovyev Jul 09 '18 at 23:42