final class · System.Diagnostics

Stopwatch

public final class Stopwatch

Measures elapsed time, from the runtime's monotonic clock in nanoseconds.

Kind
final class
Namespace
System.Diagnostics
Assembly
System.Core
Access
public, final
Source file
Runtime.ant

Inheritance

Base classes
Any Stopwatch

Properties

static i64 Frequency { get; }
#

static read-only

Ticks of GetTimestamp and ElapsedTicks per second (they are nanoseconds).

static bool IsHighResolution { get; }
#

static read-only

bool IsRunning { get; }
#

read-only

TimeSpan Elapsed { get; }
#

read-only

i64 ElapsedMilliseconds { get; }
#

read-only

i64 ElapsedTicks { get; }
#

read-only

Methods

static GetTimestamp() : i64
#

static

The clock now, in ticks of Frequency.

static GetElapsedTime(i64 start) : TimeSpan
#

static

The time from a timestamp to now, and between two.

static GetElapsedTime(i64 start, i64 end) : TimeSpan
#

static

static StartNew() : Stopwatch
#

static

Start()
#
Stop()
#
Reset()
#
Restart()
#

Private and internal members (5)

i64 started
#

private

i64 elapsed
#

private

bool running
#

private

static extern("time.precise") Now() : i64
#

private static

Nanoseconds() : i64
#

private

Nanoseconds so far.

Inherited members (5)

Source

Stopwatch as Antril, decompiled from its Larva HIR.

System.Diagnostics.Stopwatch.ant
// System.Diagnostics.Stopwatch — System.Core (decompiled from Larva HIR)

import System;

into namespace System.Diagnostics
{
	/// Measures elapsed time, from the runtime's monotonic clock in nanoseconds.
	public final class Stopwatch
	{
		i64 started;
		i64 elapsed;
		bool running;

		static extern("time.precise") Now() : i64;

		/// Nanoseconds so far.
		Nanoseconds() : i64 => running ? elapsed + Now() - started : elapsed;

		public:
			/// The clock now, in ticks of Frequency.
			static GetTimestamp() : i64 => Now();

			/// The time from a timestamp to now, and between two.
			static GetElapsedTime(i64 start) : TimeSpan => new TimeSpan((Now() - start) / 100);

			static GetElapsedTime(i64 start, i64 end) : TimeSpan => new TimeSpan((end - start) / 100);

			static StartNew() : Stopwatch
			{
				Stopwatch watch = new Stopwatch();
				watch.Start();
				return watch;
			}

			Start()
			{
				if (!running)
				{
					started = Now();
					running = true;
				}
			}

			Stop()
			{
				if (running)
				{
					elapsed += Now() - started;
					running = false;
				}
			}

			Reset()
			{
				elapsed = 0;
				running = false;
			}

			Restart()
			{
				elapsed = 0;
				started = Now();
				running = true;
			}

			/// Ticks of GetTimestamp and ElapsedTicks per second (they are nanoseconds).
			static i64 Frequency
			{
				get => 1000000000;
			}

			static bool IsHighResolution
			{
				get => true;
			}

			bool IsRunning
			{
				get => running;
			}

			TimeSpan Elapsed
			{
				get => new TimeSpan(Nanoseconds() / 100);
			}

			i64 ElapsedMilliseconds
			{
				get => Nanoseconds() / 1000000;
			}

			i64 ElapsedTicks
			{
				get => Nanoseconds();
			}
	}
}
Esc