By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,604 Members | 1,217 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,604 IT Pros & Developers. It's quick & easy.

How to calculate time for method execution?

P: n/a
how can I calculate time needed for execution of my methods in app?
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies

P: n/a
There are a number of this simply for testing purposes? You can
turn tracing on in your page directive <%@ Page Trace="True" ...%> and do
something like:


and at the bottom of the page you'll see the START and END as well as how
long it took.
Alternatively, you can profile your code using a tool such as Red-Gate's
( ANTs Profiler (full 14 day free trial)...or there are
other free ones...which should really give you deep analysis.
Finally, if you need this in more of a production scenerio, you can use
something like:

DateTime start = DateTime.Now;
TimeSpan ts = DateTime.Now.Subtract(start);
//use ts.TotalMilliseconds to get the total amount of time it took..

MY ASP.Net tutorials
"TomislaW" <to*********> wrote in message
how can I calculate time needed for execution of my methods in app?

Nov 18 '05 #2

P: n/a
For simple scenarios, I use:
public class CodeTimer : IDisposable
public CodeTimer (string msg)
_msg = msg;
if (_msg.Length > 0)
_msg += " ";

public CodeTimer () : this ("")

public void Dispose ()
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.

public void Dispose (bool disposing)
if (disposing)
string indent = new String (' ' , _depth * 4);
double totalSeconds = (DateTime.Now - _start).TotalSeconds;
Debug.WriteLineIf (totalSeconds > 0, indent + _msg + string.Format
("Elapsed time: {0}", totalSeconds));

~CodeTimer ()
this.Dispose (false);

DateTime _start = DateTime.Now;
string _msg;
static int _depth = 0;
Use it like so:

using (Com.Banshee.Utilities.CodeTimer c = new
Com.Banshee.Utilities.CodeTimer ("in CreateAdapters")) {

do stuff

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.