Calculate method execution time in Visual Studio Express (no profiler)?

I am using Visual Studio Express Edition and it does not have a profile analyzer or code.

Code with two delegates performing the same task, one using an anonymous method and one using a Lambda expression. I want to compare which one takes less time.

How can I do this in VS express? (not just for delegate for methods)

If this is a duplicate, please connect it.

thanks

I tried Like This:

/** Start Date time**/ DateTime startTime = DateTime.Now; /********do the square of a number by using LAMBDA EXPRESSIONS********/ returnSqr myDel = x => x * x; Console.WriteLine("By Lambda Expression Square of {0} is: {1}", a,myDel(a)); /** Stop Date time**/ DateTime stopTime = DateTime.Now; TimeSpan duration = stopTime - startTime; Console.WriteLine("Execution time 1:" + duration.Milliseconds); /** Start Date time**/ DateTime startTime2 = DateTime.Now; /*****do the square of a number by using ANONYMOUS EXPRESSIONS********/ returnSqr myDel1 = delegate(int x) { return x * x;}; Console.WriteLine("By Anonymous Method Square of {0} is: {1}", a, myDel1(a)); /** Stop Date time**/ DateTime stopTime2 = DateTime.Now; TimeSpan duration2 = stopTime2 - startTime2; Console.WriteLine("Execution Time 2:" + duration.Milliseconds); 

The conclusion gives:

Runtime 1: 0
Runtime 2: 0


Why is that?

+4
source share
5 answers

You can use the stopwatch class.

 Stopwatch sw = Stopwatch.StartNew(); // rest of the code sw.Stop(); Console.WriteLine("Total time (ms): {0}", (long) sw.ElapsedMilliseconds); 
+15
source

use the StopWatch class to start the timer before running the code and stop it after the code completes. Do this for both code snippets and find out what takes mroe time.

This is not an ideal solution, but it helps.

+4
source

You can use the Scenario class, which supports the simple use of start and end, with additional logging via ETW.

From their Wiki:

You can use the Scenario class to add performance tools to the application (either .NET or native C ++). The shortest description of the scenario is a "named stopwatch that can be logged at start and stop."

0
source

Just using a stopwatch class should do the trick.

0
source

Most likely, your code runs faster than the maximum resolution of your sync device. . Therefore, it reports β€œ0” milliseconds as the runtime for both parts of the code: the amount of time that has passed has not been determined. For some people, this may be enough to conclude it, it probably doesn't matter how you do it.

However, if it is important to get an accurate comparison of the speed of each method, you can try to run the code in a narrow loop. Essentially, do what you do about 100 or 1000, or even a million times in a row, but many of them require a sufficient amount of time to be recorded on your time tracking device. Obviously, there will be nothing in your final code to start the procedure once or even several times, but at least this will give you an idea of ​​the relative speed of each option.

0
source

All Articles