A 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 Start method, then eventually call the Stop method, and then you check elapsed time using the Elapsed property.
If you want to measure the time required for your programs written in the C# language. This could be for a micro-benchmark or for routine and continuous performance monitoring.
Example
First sample:
In this sample you must create Stopwatch as an instance. This makes it useful in multithreaded applications or websites.
using System;
using System.Diagnostics;
using System.Threading;
class Program
{
static void Main(string[] args)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
Thread.Sleep(10000);//or do some work
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
Console.WriteLine("RunTime " + elapsedTime);
}
}
Second sample:
In this sample we use the StartNew method. This uses a creational design pattern to return a new instance from a static type method. It eliminates typing and simplifies your code.
class Program
{
static void Main(string[] args)
{
// Create new stopwatch
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
// Do something
// Stop timing
stopwatch.Stop();
// Write the results
TimeSpan ts = stopWatch.Elapsed;
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
Console.WriteLine("RunTime " + elapsedTime);
}
}
For more details please visits here.
Sharing real-world experiences about C#, Dynamics CRM, Dynamics 365, Dynamics NAV/Business Central, SharePoint,PowerBI,ASP.net and more...
Subscribe to:
Post Comments (Atom)
Method 'StartWorkflowOnListItem' in type 'Microsoft.SharePoint.WorkflowServices.FabricWorkflowInstanceProvider'
Exception: Method 'StartWorkflowOnListItem' in type 'Microsoft.SharePoint.WorkflowServices.FabricWorkflowInstanceProvider'...
-
How to get the MAC address of system using Asp.net/C# Now I want to share with you some simple but important for our rapid development. ...
-
Last night I have got this mail from Microsoft MVP award program manager. “Dear Ahsan Murshed, Congratulations! We are pleased to presen...
-
I have little bit confusion about HTTP Handler and HTTP Module. Then I move to community and get a huge response. After studying for a while...
No comments:
Post a Comment