Stopwatch
This class is a System.Diagnostics.Stopwatch on operating systems that support it. On those that don't,
it replicates the functionality at the resolution supported.
using System;
namespace NUnit.Framework.Compatibility
{
public class Stopwatch
{
private const long TicksPerMillisecond = 10000;
private const long TicksPerSecond = 10000000;
private long _elapsedMilliseconds;
private bool _isRunning;
private long _startMilliseconds = GetTimestamp();
public long ElapsedMilliseconds => (_elapsedMilliseconds + GetTimeSinceLastStart()) / 10000;
public bool IsRunning => _isRunning;
public static long Frequency => 10000000;
public static bool IsHighResolution => false;
public static long GetTimestamp()
{
return DateTime.UtcNow.Ticks;
}
public void Reset()
{
_elapsedMilliseconds = 0;
_isRunning = false;
}
public void Start()
{
if (!_isRunning) {
_isRunning = true;
_startMilliseconds = GetTimestamp();
}
}
public static Stopwatch StartNew()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
return stopwatch;
}
public void Stop()
{
if (_isRunning) {
_elapsedMilliseconds += GetTimeSinceLastStart();
_isRunning = false;
}
}
public override string ToString()
{
return base.ToString();
}
private long GetTimeSinceLastStart()
{
if (_isRunning)
return GetTimestamp() - _startMilliseconds;
return 0;
}
}
}