1

I want to check the amount of time it takes for a specific function to be performed ,so that I will add accurate time delay on my program... I tried to write this before and after my function:

public static String (this DateTime value)
{
   return value.ToString("mmssffff");
}

Then I calculated the difference between both results, and I get 350.I do not know if it microseconds or milliseconds...

Do you know the meaning of the result?(350) Do you have another idea to do this? Thanks!

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Tehila
  • 73
  • 1
  • 7

3 Answers3

3

Use Stopwatch for performance profiling.

 Stopwatch sw = new Stopwatch();
  sw.Start();
  SayHello();
  sw.Stop();
  Console.WriteLine("Total time elapsed {0} millseconds", sw.Elapsed.TotalMilliSeconds);
Tilak
  • 30,108
  • 19
  • 83
  • 131
2

I normally use something like this:

Stopwatch sw = Stopwatch.StartNew();
// rest of the code
sw.Stop();
Console.WriteLine("Total time (ms): {0}", sw.ElapsedMilliseconds);
Wilton
  • 218
  • 2
  • 11
0

You can use profilers for this task. In many cases the single method takes a lot of time but a sub-method which should be executed instantly takes most of the time. In fact, I was working on a Java project before and I have observed that it took unreasonably much time to work out the solution. I have used a profiler there and have seen that a simple object instantiation took most of the time. The problem was, of course, that the Java library initialized a hole lot of things because of that constructor, so I have chosen an alternative instead. So, I think you should use a profiler, read more here and here to start working with C# profilers.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175