0

I am very curious about run time/speed about codes in ms visual studio.

As instance,

1. code:

byte[] buffer = new byte[4096]; var p = System.IO.Directory.GetFiles(directoryName); 

2. code

var entry = new ZipEntry(Path.GetFileName(file)); entry.DateTime = DateTime.Now; 

If i run 1. code ı want to see "it's running time/speed 0.03 seconds"

If i run 2. code ı want to see "it's running time/speed 0.06 seconds"

Is there anything to calculate running time/speed of codes in c# without using timer ?

Any help will be appreciated.

Thanks.

3
  • Are you profiling the allocation speed of the CLR with the byte[] example? ... what purpose does that serve? ... Also, look at the Stopwatch. Commented Apr 9, 2014 at 14:12
  • If i debug rows , i want to calculate running time/speed of debugging duration etc etc.. Commented Apr 9, 2014 at 14:13
  • 2
    Is this just for debugging/profiling? visual studio has a very handy performance analysis tool.. Commented Apr 9, 2014 at 14:13

3 Answers 3

4

The best thing to use for quickly profiling code is Stopwatch in System.Diagonstics

var sw = Stopwatch.StartNew(); ///.... stuff sw.Stop(); sw.ElapsedMilliseconds; 

If this is something you may want to use in production, i'd recommend: http://miniprofiler.com/

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

4 Comments

There is no anything as "StopWatch" in Winforms c# ? If i write StopWatch , there is no dll for StopWatch why can not i use it ?
You will have to add a reference to System.Diagostics
its Stopwatch not StopWatch
@DaveBish there is no System.Diagnostics lib to reference, System.Diagnostics is just a namespace part of the core lib (System.dll).
3

You can use a Stopwatch for benchmarking code

var sw = Stopwatch.StartNew(); var entry = new ZipEntry(Path.GetFileName(file)); sw.Stop(); Console.WriteLine("Time to zip: {0}", sw.Elapsed); 

You could create a helper method if you intend on doing using it a lot

public static TimeSpan Benchmark(Action action) { var sw = Stopwatch.StartNew(); action(); sw.Stop(); return sw.Elapsed; } ... var timeTaken = Benchmark(() => /* run some code */) 

3 Comments

There is no anything as "StopWatch" in Winforms c# ? If i write StopWatch , there is no dll for StopWatch why can not i use it ?
@user3409638 yes there is, it's in the System.Diagnostics namespace, just add a using to the top of your app. Or alternatively, right click Stopwatch in the IDE and select Resolve and it will do that for you.
@user3409638 it's there, it's been part of the core framework since .NET 2.0.
2

Typically one would use the stopwatch.startnew, stop, elapsedtime methods. There are some profiling tools on the market, and microsoft also has its own built-in performance suite. The following link is a tutorial on how to set it up. http://msdn.microsoft.com/en-us/library/ms182372.aspx

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.