49

I would like to test how fast does my projects function work. It would be great if there were a possibility to mark slow places of my function as well so I can change my code to increase performance. I'm using Microsoft Visual Studio 2012 and I know that there is a build-in testing tool but I don't really know where to find it and probably how to use it as well. It would be great if someone could help me with this issue.

4 Answers 4

68

Visual Studio shows execution time in milliseconds for each line or when Run To Click is used during debugging session, which can give some rough idea of the statement execution time:

For simple measurements Stopwatch class can be a handy option too.

For more advanced scenarios there is a built-in profiler in Visual Studio Analyze CPU usage without debugging in the Performance Profiler.

And for precise performance measurements there is an excellent and powerful tool BenchmarkDotNet.

Sign up to request clarification or add additional context in comments.

4 Comments

VS 2017 now shows execution time in milliseconds for each line where is this? I'd ran the Sampling and Instrumentation profilers and am not seeing this.
@DouglasGaskell it is in the editor during the debug. Here is a gif from the article I referenced msdnshared.blob.core.windows.net/media/2017/03/….
I mean, this is nice, but how can one get a report of this and then sort by execution time? I don't want to highlight every line like this to get a complete project analysis.
@ScottN the performance profiler allows sorting by execution time, please check learn.microsoft.com/en-us/visualstudio/profiling/… for more details. Let me update the answer with the link.
33

In Visual Studio 2013

In the top menu, go to ANALYZE -> Performance and Diagnostics

Check the Performance Wizard to calculate time required. Click- Start. Now select - Instrumentation to measure Function calls and count. Click next. Select- One or more available projects in that name of your project will be shown. Click next. Again click next. Check- Launch profiling after the wizard finishes. Now Click Finish button.


In Visual Studio 2017

In the top menu, go to ANALYZE -> Performance Profiler

Check the Performance Wizard to calculate time required. Click- Start. Now select - Instrumentation to measure Function calls and count. Click next. Select- One or more available projects in that name of your project will be shown. Click next. Again click next. Check- Launch profiling after the wizard finishes. Now Click Finish button.

However, you may also prefer to use the Visual Studio Command Window and VSPerfCmd to do your profiling, as it has features the Performance Profiler does not. See: https://msdn.microsoft.com/en-us/library/dd255376.aspx

If you run into problems using Performance Wizard on Windows 10, please see: CPU sampling method is disabled in performance profiler.

In Visual Studio 2019

In the menu bar, go to ANALYZE -> Performance Profiler(it is same as VS2017) or simply press Alt+F12 Change target if you want from Change Target Dropdown, it allows to analyze Visual studio projects as well currently executed process, installed executable files ASP.NET application from IIS or currently running application.

2 Comments

In my VS2019, it's Alt+F2, not F12
You may find it at Debug -> Performance Profiler in VS2019
4

You find the Performance Wizard in the Analyze menu, if you have a sufficiently good version of Visual Studio (Professional?).

It measures the time spend in each method that is used and the statistics give you a good overview over possible bottlenecks.

1 Comment

Yes, it's Professional.
4

Visual Studio contains a lot of good built-in tools for profiling, and many others are available as plugins (also free).

http://msdn.microsoft.com/en-us/library/ms182372.aspx This MSDN resource could be a good starting point.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.