I am not familiar with c# stopwatch class. I am trying to find ellapsed time for a function. When I call a function again I need to get the total time since from last call. How Should I do it?
I thought something like below;
`
public double CalculateAngle(double Theta_dot, double Psi_dot)
{
mywatch.Stop();
var time = mywatch.ElapsedMilliseconds / 1000;
mywatch.Start();
`
CodePudding user response:
If you want to measure the time since the last time the method was called then use Restart:
Use Restart to stop current interval measurement and start a new interval measurement.
public double CalculateAngle(double Theta_dot, double Psi_dot)
{
TimeSpan ts = sw.Elapsed;
mywatch.Restart();
}
Depending on what you're trying to achieve you'd put restart at the start or end of the method...
CodePudding user response:
You can store execution times in a global list.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
public class Program
{
public static List<long> executionTimes = new List<long>();
public static void Main()
{
Stopwatch watch = new Stopwatch();
for (int i = 0; i < 10; i )
{
watch.Start();
MyFunction();
var elapsed = watch.ElapsedMilliseconds;
executionTimes.Add(elapsed);
}
Console.WriteLine(String.Join("; ", executionTimes));
}
public static async Task MyFunction()
{
//your function...
}
}
If you need, you can use Stop() or Restart() methods of watch.