From the course: Visual Studio: Advanced Debugging Tools

View CPU usage by function - Visual Studio Tutorial

From the course: Visual Studio: Advanced Debugging Tools

Start my 1-month free trial

View CPU usage by function

- [Instructor] One of the benefits of working with the profiler is that I can save the state of a session and open it later when I want to review that session. And that's what we'll look at in this video. I saved the last profile session to a file on my hard drive, and I can open that whenever I need to refresh my memory of what happened in that profiling session by going to file, and finding that file and opening it. What we see at the top is some statistics, I did a profiler for about 40 seconds. And here I see the CPU usage on all my processors, I've got eight cores on this machine, so there was a little bit of action happening at the beginning, and I can select that slice of time, and get a detailed view of that slice of time. And then I can see over here, at this spot, there was a lot of CPU usage. Now why do we have this tool? It's so that we can determine when our CPU is being used correctly and doing useful work, and when it's being used incorrectly, and it's wasting cycles. There's nothing wrong with having an application that uses your CPU, that's why we have them. But we don't want to spend time running the CPU when it's not necessary. So, what tools do I have here, let me give you a brief overview and then we'll drill down into this. So I can see a time slice, and then I can see all the function calls and how much time was spent in each of those function calls on the CPU. So these ones down here, there's really not much time spent, but there's a lot spent up here in this area. I can double-click on one of these, or click on this icon here to open up a visual view of that. So I can see the calling functions, the current function, and the called functions. So if that's important to me I can see where my code is moving during this profile session. And I can also look at it in call or callee view, which I'm looking at right now, I can look at it in what's called the call tree, I can see a tree view of that, and then I can look at it as modules, which modules were called. Notice I've got a second tab here, here's the original tab, this is the substantive tab, so when I chose this icon, or double-click on it, I get a second tab with a detailed section. Now let's take a look at how we could use this to analyze my code. We're looking at this spike here, something happened at this point in time. And here's the one that stood out as interesting to me. Look at this call here. Get_now, that's the getter for the now property on the date time class, and it was 88% of my CPU time was spent calling that function. That function is part of the getter. Now why is that? Let's go look at my code. Well here, I have this while loop, so I'm running this code here in the while loop, and every time I loop, I check the current time against a future time. And of course this is calling the getter on the now property. So this was unexpected. I knew that my code was doing some work, but I wasn't, maybe we didn't know that it was because of this date time now property. And that's the whole point of having this information, is you can use it to examine your code, and if you can figure out where your CPU is being used, and is it doing worthwhile work in that area of your code.

Contents