Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

System.Diagnostics.Stopwatch: practical reference

System.Diagnostics.Stopwatch: practical reference: Stopwatch measures elapsed intervals at Stopwatch.Frequency; when Stopwatch.IsHighResolution is true it uses a high-resolution hardware or operating-system counter, otherwise it uses the system timer.

Version: .NET API; verified on .NET 10.0.14

Syntax

System.Diagnostics.Stopwatch /* select a documented member or constructor */

Stopwatch measures elapsed intervals at Stopwatch.Frequency; when Stopwatch.IsHighResolution is true it uses a high-resolution hardware or operating-system counter, otherwise it uses the system timer.

Use it for diagnostic timeouts and duration measurements, not for wall-clock timestamps.

A microbenchmark needs warm-up and repeated trials because one measurement includes environmental noise.

Elapsed is nonnegative; Start after Stop continues accumulating, while Restart clears and starts again.

The complete program actually uses System.Diagnostics.Stopwatch; its exact expected stdout is True.

Examples

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

var watch = Stopwatch.StartNew();
watch.Stop();
Console.WriteLine(watch.Elapsed >= TimeSpan.Zero);

Result

True

The complete program actually uses `System.Diagnostics.Stopwatch`; its exact expected stdout is `True`.

Sources