Any class in C # that can tell me a tick, seconds consumed by a function

Is there a class in C # that can give me clock ticks, seconds consumed by a method? I assume that I have two wrappers that have functionality around the function so that ticks and seconds are busy.

+3
source share
4 answers

You can use a class System.Diagnostics.Stopwatch.

Stopwatch sw = new Stopwatch();
sw.Start();

// Your method call here...

sw.Stop();

// Get the elapsed time
TimeSpan elapsed = sw.Elapsed;

Here you can use TimeSpan.Tickseither TimeSpan.TotalSecondsto determine past ticks or past seconds, respectively.

, , "" , ( , , , - - ..):

public static T ExecuteWithElapsedTime<T>(Func<T> function, out TimeSpan elapsedTime)
{
   T rval;

   Stopwatch sw = new Stopwatch();
   sw.Start();
   rval = function();
   sw.Stop();
   elapsedTime = sw.Elapsed;

   return rval;
}

( myFunc - , int):

TimeSpan elapsed;
int result = ExecuteWithElapsedTime(myFunc, out elapsed);

, , .

+11

:

using System.Diagnostics;

...

var sw = Stopwatch.StartNew();
DoYaThing();
Console.WriteLine("{0} Elapsed", sw.Elapsed);
+3

...

, iirc a TimeSpan .

0

[System.TimeSpan] .

0

Source: https://habr.com/ru/post/1779409/


All Articles