Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Stopwatch class in C#
The Stopwatch class in C# is used to measure elapsed time with high precision. It provides accurate timing measurements for performance monitoring and benchmarking applications. The class is found in the System.Diagnostics namespace.
Syntax
Following is the syntax for creating and starting a Stopwatch −
Stopwatch stopwatch = new Stopwatch(); stopwatch.Start();
Alternatively, you can create and start in one line −
Stopwatch stopwatch = Stopwatch.StartNew();
Key Properties and Methods
| Property/Method | Description |
|---|---|
Start() |
Starts measuring elapsed time |
Stop() |
Stops measuring elapsed time |
Reset() |
Stops timing and resets elapsed time to zero |
Restart() |
Resets and starts the stopwatch |
ElapsedTicks |
Gets elapsed time in timer ticks |
ElapsedMilliseconds |
Gets elapsed time in milliseconds |
Elapsed |
Gets elapsed time as a TimeSpan object |
Using Stopwatch for Basic Timing
The following example demonstrates basic Stopwatch usage to measure elapsed time −
using System;
using System.Diagnostics;
using System.Threading;
public class Demo {
public static void Main() {
var sw = Stopwatch.StartNew();
// Simulate some work
Thread.Sleep(1000);
sw.Stop();
Console.WriteLine("Elapsed Ticks: " + sw.ElapsedTicks);
Console.WriteLine("Elapsed Milliseconds: " + sw.ElapsedMilliseconds);
Console.WriteLine("Elapsed Time: " + sw.Elapsed);
}
}
The output of the above code is −
Elapsed Ticks: 10001234 Elapsed Milliseconds: 1000 Elapsed Time: 00:00:01.0001234
Using Stopwatch to Measure Function Performance
Here's an example that measures the execution time of different operations −
using System;
using System.Diagnostics;
using System.Linq;
public class PerformanceTest {
public static void Main() {
// Test array creation and initialization
var sw1 = Stopwatch.StartNew();
int[] numbers = Enumerable.Range(1, 1000000).ToArray();
sw1.Stop();
// Test array sum calculation
var sw2 = Stopwatch.StartNew();
long sum = numbers.Sum();
sw2.Stop();
Console.WriteLine("Array creation took: " + sw1.ElapsedMilliseconds + " ms");
Console.WriteLine("Sum calculation took: " + sw2.ElapsedMilliseconds + " ms");
Console.WriteLine("Total sum: " + sum);
}
}
The output of the above code is −
Array creation took: 45 ms Sum calculation took: 2 ms Total sum: 500000500000
Using Start, Stop, and Reset Methods
This example shows how to control the Stopwatch with different methods −
using System;
using System.Diagnostics;
using System.Threading;
public class StopwatchControl {
public static void Main() {
Stopwatch sw = new Stopwatch();
// Start timing
sw.Start();
Thread.Sleep(500);
Console.WriteLine("After 500ms: " + sw.ElapsedMilliseconds + " ms");
// Stop timing
sw.Stop();
Thread.Sleep(300); // This won't be counted
Console.WriteLine("After stop (300ms sleep): " + sw.ElapsedMilliseconds + " ms");
// Resume timing
sw.Start();
Thread.Sleep(200);
Console.WriteLine("After resume + 200ms: " + sw.ElapsedMilliseconds + " ms");
// Reset and restart
sw.Restart();
Thread.Sleep(100);
Console.WriteLine("After restart + 100ms: " + sw.ElapsedMilliseconds + " ms");
}
}
The output of the above code is −
After 500ms: 500 ms After stop (300ms sleep): 500 ms After resume + 200ms: 700 ms After restart + 100ms: 100 ms
Common Use Cases
-
Performance benchmarking − Comparing execution times of different algorithms.
-
Timeout implementation − Measuring if an operation exceeds a time limit.
-
Profiling applications − Identifying performance bottlenecks in code.
-
Game development − Measuring frame times and animation durations.
Conclusion
The Stopwatch class in C# provides high-precision timing capabilities essential for performance measurement and benchmarking. It offers multiple time representations (ticks, milliseconds, TimeSpan) and flexible control methods for accurate timing in various scenarios.
