-1

I am developing a program in LinqPad that will scrub through files and return a csv of some information I am curious about. I need to track the time it takes for certain processes to execute and was trying to use the Stopwatch method to do so.

Ultimately what I'd like to do is something similar to this:

void main()
{
    T runTime = Timer(someProcess());
}

void someProcess(){...}

public static class Diagnostics
{
     …
     public static T Timer(???)
     {
         … (execute whatever we're timing)
         return T timeTookToExe;
     }
}
  • Wait, after re-reading your question you say: `"...was trying to use the Stopwatch method..."` what does this mean? Why isnt [`Stopwatch`](https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch(v=vs.110).aspx) working for you? Your code does not indicate any attempt with Stopwatch – maccettura Jul 16 '18 at 19:19
  • What's the question? – Alejandro Jul 16 '18 at 19:28

1 Answers1

3

I'm not sure what T is supposed to be, but you could make Timer receive a delegate:

public static TimeSpan Timer(Action action)
{
     Stopwatch sw = new Stopwatch();
     sw.Start();
     action.Invoke();
     sw.Stop();

     return sw.Elapsed;
}

And then to use it, you'd do something like:

TimeSpan runTIme = Timer(() => someProcess());
itsme86
  • 19,266
  • 4
  • 41
  • 57