Understanding StopWatch Class In C#
2012-04-01 20:28
417 查看
Stopwatch instance can measure elapsed time for one interval, or the total of elapsed time across multiple intervals. In a typical Stopwatch scenario, you call the Startmethod, then eventually call the Stop method, and then you check elapsed time using the Elapsed property. (From MSDN)
Let say i want to find out Total Time a Process has been Running.
Like
1. How many Minutes or Second it took to insert 1000 record in Database?
2. How many time it took to update the Database ? etc ..
I provide a very Basic Example with a Console Application ..
The Stopwatch Class has two Primary Methods 1. Start() 2. Stop() and One Property that is Elapsed .
The Code:
As You can See I have started our Stopwatch Just Before My loop has started.
Now the Loop will Do its work ..
After That As Soon As we are out sw.Stop has been Called.
I have Just Recorded The Timing The Loop Took To Complete.
The Output Will Look SomeThing Like These:
Let say i want to find out Total Time a Process has been Running.
Like
1. How many Minutes or Second it took to insert 1000 record in Database?
2. How many time it took to update the Database ? etc ..
I provide a very Basic Example with a Console Application ..
The Stopwatch Class has two Primary Methods 1. Start() 2. Stop() and One Property that is Elapsed .
The Code:
using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; namespace StopWatchExample { class Program { static void Main(string[] args) { Stopwatch sw = new Stopwatch(); sw.Start(); Console.WriteLine("Stop Watch Start....\n"); for (int i = 1; i <= 5; i++) { Console.WriteLine(i + "\n"); } sw.Stop(); Console.WriteLine("Stop Watch Stopped:"); Console.WriteLine("Total Time: {0}", sw.Elapsed); Console.ReadLine(); } } }
As You can See I have started our Stopwatch Just Before My loop has started.
Now the Loop will Do its work ..
After That As Soon As we are out sw.Stop has been Called.
I have Just Recorded The Timing The Loop Took To Complete.
The Output Will Look SomeThing Like These:
相关文章推荐
- Implement a DataSet SELECT DISTINCT Helper Class in Visual C# .NET
- [Tip]dispose resource in C++/CLI using a C# class
- ZipStorer - A Pure C# Class to Store Files in Zip
- Class To Change Wallpaper In Xp,Vista,Windows 7 From C#.Net
- HOW TO: Implement a DataSet JOIN helper class in Visual C# .NET(DataSetHelper)
- java.lang.Object in Java and System.Object class in C#
- C#.NET Class扩展 ToJson/Select/IsBetween/In/Compress/Serialize
- Understanding Predicate Delegates in C#
- (转)Implement a DataSet SELECT DISTINCT Helper Class in Visual C# .NET
- Inheriting From a Native C++ Class in C#
- understanding in the memory management in C#
- Static Class in C#
- Sealed Class in C#
- Understanding Properties in C#
- Class for the use of RegQueryInfoKey pinvoke interop in C#
- ataTable 去除重复项 HOW TO: Implement a DataSet SELECT DISTINCT Helper Class in Visual C# .NET
- Part 34 to 35 Talking about multiple class inheritance in C#
- using class in c#(simple)
- the Differences between abstract class & interface in C#接口和抽象类的区别
- HOW TO: Implement a DataSet SELECT DISTINCT Helper Class in Visual C# .NET