2

I am trying to understand profiler report and use it for optimise my code. The problem I have is I do not see any problem with the case software highlighted !

For instance:

14.2%    public int vbar.X {get{return vbar.x;} set {vbar.x = value;}}

// time

for (int t = 2; t < 1000000; t++)
{

// main calculations
    for (int i = 2; i < 800; i++)
                    {
0.5%                 for (int j = 2; j < 800; j++)
                        {                              
                                vbar.X = 0.0;
34%                             vbar.Y = 0.0;
                              // some culculations for vbar.X and vbar.Y  
lap.X = ((((theSpace.TheCells[i + 1, j, 0].Velocity.X - theSpace.TheCells[i, j, 0].Velocity.X) / theSpace.TheCells[i + 1, j, 0].Nondx) - ((theSpace.TheCells[i, j, 0].Velocity.X - theSpace.TheCells[i - 1, j, 0].Velocity.X) / theSpace.TheCells[i, j, 0].Nondx)) * (2 / (theSpace.TheCells[i + 1, j, 0].Nondx + theSpace.TheCells[i, j, 0].Nondx)))
                                            + (((theSpace.TheCells[i, j + 1, 0].Velocity.X - theSpace.TheCells[i, j, 0].Velocity.X) / theSpace.TheCells[i, j + 1, 0].Nondy) - ((theSpace.TheCells[i, j, 0].Velocity.X - theSpace.TheCells[i, j - 1, 0].Velocity.X) / theSpace.TheCells[i, j, 0].Nondy)) * (2 / (theSpace.TheCells[i, j + 1, 0].Nondy + theSpace.TheCells[i, j, 0].Nondy));
                                        vbar.X = theSpace.TheCells[i, j, 0].Velocity.X - theSpace.Dt * (adv.X - Math.Sqrt(slnParameter.Pr / slnParameter.Ra) * lap.X);

lap.Y = ((((theSpace.TheCells[i + 1, j, 0].Velocity.Y - theSpace.TheCells[i, j, 0].Velocity.Y) / theSpace.TheCells[i + 1, j, 0].Nondx) - ((theSpace.TheCells[i, j, 0].Velocity.Y - theSpace.TheCells[i - 1, j, 0].Velocity.Y) / theSpace.TheCells[i, j, 0].Nondx)) * (2 / (theSpace.TheCells[i + 1, j, 0].Nondx + theSpace.TheCells[i, j, 0].Nondx)))
                                            + (((theSpace.TheCells[i, j + 1, 0].Velocity.Y - theSpace.TheCells[i, j, 0].Velocity.Y) / theSpace.TheCells[i, j + 1, 0].Nondy) - ((theSpace.TheCells[i, j, 0].Velocity.Y - theSpace.TheCells[i, j - 1, 0].Velocity.Y) / theSpace.TheCells[i, j, 0].Nondy)) * (2 / (theSpace.TheCells[i, j + 1, 0].Nondy + theSpace.TheCells[i, j, 0].Nondy));
                                        vbar.Y = theSpace.TheCells[i, j, 0].Velocity.Y - theSpace.Dt * (adv.Y - Math.Sqrt(slnParameter.Pr / slnParameter.Ra) * lap.Y - theSpace.TheCells[i, j, 0].Temperature);


                        }
                   }
}

... Based on this sample, my code has problem with the following lines : all these variables are integers: vbar.X = 0.0, int j = 2 and j++ . As you can see in the second picture.

How should I fix these problems? or what part of these loops has problem ? What is the other way of coding this part of code (vbar.X = 0.0) which have 39% inclusive time? Interestingly, the time needed for getting result by profiler is half of amount that code need to run !

enter image description here enter image description here

//////////////////////////////////////////////////////////////////////////////////////////////////////

I found the answer for part of my question. Regarding this part (vbar.X = 0.0) it was incorrect because I can put it out side of my loops and avoid repeating it in every iteration.

//////////////////////////////////////////////////////////////////////////////////////////////////////

Mehdi
  • 211
  • 2
  • 11
  • Do you have any indication that this particular loop is a bottleneck in your real application? If you're just microbenchmarking this loop, it may not mean much at all. What's the type of `vbar`? Is `TotalNY` a property or a constant? – Jon Skeet Jul 07 '14 at 05:51
  • This is the only Hot path that profiling report gave me. All those variables are integer. – Mehdi Jul 07 '14 at 05:55
  • But the hot path when testing what? The whole application? Is `vbar.Y` a property? If so, what does it do? – Jon Skeet Jul 07 '14 at 06:17
  • this is a profiling of my whole code. vbar.Y is a double variable. I do not understand how I can make use of this report. For example it says that vbar.Y variable has high time consumption and the property of this variable is hot path. So what? what can I do for that? It is obvious that this variable is my main variable and all calculations have been done for that. Can anybody tell my how it works and how you usually use these comments ? – Mehdi Jul 07 '14 at 10:27
  • That does seem very odd, given that you're just assigning a 0 value to it. It's hard to say much more without seeing a short but complete program demonstrating this... – Jon Skeet Jul 07 '14 at 10:28
  • interestingly, the time needed for running my code is more than the time needed by profiler !! at least half, how it happens? how I can do that for my code to need less time? – Mehdi Jul 07 '14 at 10:31
  • I'm afraid I don't know what you mean, but I don't think anyone's going to be able to help you at the moment. (At least, I can't.) If you could give me something to reproduce, that would help a lot... – Jon Skeet Jul 07 '14 at 10:32
  • Can anyone help me about this problem and how I should handle it? I update the info in question to be more clear. – Mehdi Jul 07 '14 at 11:28
  • The only thing your program does is store 0 in vbar.X and .Y, and increment j and compare it to 800. Are you surprised to see that is what it's doing? – Mike Dunlavey Jul 07 '14 at 11:56
  • No, I just did not want to make the code complicate. vbar.X and .Y store zero first and then after calculations that put now, they get the final result. – Mehdi Jul 07 '14 at 12:03
  • 3
    Profilers have difficulty telling you what's going on within a line of code. You can spread out those calculations onto multiple lines of code. It will not slow down the calculation, but it will be easier to read, and the profiler could give you finer insight. (Also, the code looks like some physics professor showed you how to write it. Consistent indentation and readability are *important* - they help you find your mistakes - and everybody makes mistakes.) – Mike Dunlavey Jul 07 '14 at 12:12
  • yes, this code simulates fluid motion. At first I hadn't any problem with it, but when I increase my mesh size the calculation speed decrease drastically. – Mehdi Jul 07 '14 at 12:24
  • Looks to me that you are operating the profiler in sampling mode. That provides only a course view of what code in your program is taking time, it is a statistical approach that cannot give you a detailed enough breakdown. Getting accurate profile results for individual statements requires instrumentation mode. Not often practical since it slows down the program a great deal. Your algorithm has O(n^2) complexity so a drastic slowdown for bigger problems is expected and inevitable. – Hans Passant Jul 07 '14 at 12:35
  • This profiling includes instrumentation mode. In the new version of VS pro, performance wizard includes CPU sampling, instrumentation, Net memory allocation and resource contention. – Mehdi Jul 07 '14 at 12:48
  • 1
    @Hans, as long as we're sharing opinions about sampling vs. instrumentation, I much prefer sampling, provided it samples the entire call stack and reports inclusive percent at line (or finer) resolution. It doesn't give many digits of timing precision, but that's not the point - it pinpoints optimization opportunities. [*Here is more on that.*](http://stackoverflow.com/a/12147841/23771) – Mike Dunlavey Jul 07 '14 at 17:23

1 Answers1

1

This is likely a manifestation of inaccuracy in the source line highlighting functionality of the profiler. If possible, I'd try it in the Visual Studio "14" preview to see if it's been fixed since we made some improvements in this area recently.

That said, when in release mode, source line mappings are not 100% accurate because of optimizations that can occur, especially for .NET code. As a general rule, in cases where the highlighted lines disagree with what's the call tree view says, favor the call tree.

mandaleeka
  • 6,577
  • 1
  • 27
  • 34