Thursday, January 26, 2012

Stopwatch class to determine the execution time for an application

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.

No comments:

Method 'StartWorkflowOnListItem' in type 'Microsoft.SharePoint.WorkflowServices.FabricWorkflowInstanceProvider'

Exception: Method 'StartWorkflowOnListItem' in type 'Microsoft.SharePoint.WorkflowServices.FabricWorkflowInstanceProvider'...