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
C# Program to set the timer to zero
The Stopwatch class in C# provides precise timing capabilities. To set the timer to zero, you can use the Restart() method, which stops the current timer and immediately starts it again from zero.
Syntax
Following is the syntax to create and start a Stopwatch −
Stopwatch stopwatch = Stopwatch.StartNew();
Following is the syntax to restart the timer and set it to zero −
stopwatch.Restart();
Using Restart() Method
The Restart() method combines two operations: it stops the current timer and immediately starts a new timing session from zero. This is equivalent to calling Reset() followed by Start() −
Example
using System;
using System.Threading;
using System.Diagnostics;
public class Demo {
public static void Main() {
Stopwatch s = Stopwatch.StartNew();
Thread.Sleep(500);
Console.WriteLine("Before restart: " + s.Elapsed);
// restart - sets timer to zero and starts again
s.Restart();
// begin timing again from zero
Thread.Sleep(500);
Console.WriteLine("After restart: " + s.Elapsed);
}
}
The output of the above code is −
Before restart: 00:00:00.5004937 After restart: 00:00:00.5003421
Comparison of Restart vs Reset + Start
The Restart() method is functionally equivalent to calling Reset() followed by Start(), but more efficient −
Example
using System;
using System.Threading;
using System.Diagnostics;
public class StopwatchComparison {
public static void Main() {
// Method 1: Using Restart()
Stopwatch sw1 = Stopwatch.StartNew();
Thread.Sleep(300);
Console.WriteLine("Before Restart(): " + sw1.Elapsed);
sw1.Restart();
Thread.Sleep(200);
Console.WriteLine("After Restart(): " + sw1.Elapsed);
Console.WriteLine();
// Method 2: Using Reset() + Start()
Stopwatch sw2 = Stopwatch.StartNew();
Thread.Sleep(300);
Console.WriteLine("Before Reset+Start: " + sw2.Elapsed);
sw2.Reset();
sw2.Start();
Thread.Sleep(200);
Console.WriteLine("After Reset+Start: " + sw2.Elapsed);
}
}
The output of the above code is −
Before Restart(): 00:00:00.3003254 After Restart(): 00:00:00.2001876
Common Use Cases
The Restart() method is particularly useful when you need to time multiple operations or iterations without creating new Stopwatch instances −
Example
using System;
using System.Threading;
using System.Diagnostics;
public class TimingOperations {
public static void Main() {
Stopwatch timer = new Stopwatch();
for (int i = 1; i <= 3; i++) {
timer.Restart(); // Reset to zero and start
// Simulate some work
Thread.Sleep(100 * i);
Console.WriteLine($"Operation {i} took: {timer.Elapsed}");
}
}
}
The output of the above code is −
Operation 1 took: 00:00:00.1001234 Operation 2 took: 00:00:00.2002456 Operation 3 took: 00:00:00.3003678
Conclusion
The Restart() method is the most efficient way to reset a Stopwatch timer to zero and start timing again. It combines the functionality of Reset() and Start() methods in a single atomic operation, making it ideal for timing multiple operations or iterations.
