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'...
-
Exception: Method 'StartWorkflowOnListItem' in type 'Microsoft.SharePoint.WorkflowServices.FabricWorkflowInstanceProvider'...
-
Today I have added Google Map my asp.net application successfully !! Don't worry it's not a big deal. Very easy and you will find a ...
-
Today I want share with you a very interesting tips using Response.Redirect(). In asp.net there are various ways to move from one page to an...
No comments:
Post a Comment