Archive

Archive for October 20, 2010

Have an operation to measure? Use Stopwatch()

October 20, 2010 2 comments

Quite often we run into situations where we need to measure the elapsed time of a method/operation, and there are number of ways to do it. The simplest way to do is by using a Stopwatch. This class is available under the namespace System.Diagnostics.

To show you a demo, I have used a simple app which calculates the time taken to load a website on your local machine.

namespace Nish.Stopwatch
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("WebAddress> ");
            Console.WriteLine("Your website took about " + LoadWebsite(Console.ReadLine()).Seconds + " seconds to load on your machine");
            Console.ReadLine();
        }

        /// <summary>
        /// Gets the load time of a website
        /// </summary>
        /// <param name="websitePath">Path in the format: e.g. http://nnish.com </param>
        /// <returns>Time taken</returns>
        private static TimeSpan LoadWebsite(string websitePath)
        {
            System.Diagnostics.Stopwatch stpWatch = new System.Diagnostics.Stopwatch();
            stpWatch.Start();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(websitePath);

            // execute the request
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            // Read the data in the stream
            StreamReader streamReader = new StreamReader(response.GetResponseStream());
            // s – will have your html content, I am not using this for now
            string s = streamReader.ReadToEnd();
            stpWatch.Stop();
            
            return stpWatch.Elapsed;
        }
    }
}

 

Time took for my website to load:

image

Look at the time Google took:

image

And now this was out of curiosity:

image

Please note: The load time depends on various factors like network speed, server location,  page content etc. So this data should not be treated as accurate. The idea of having this example is to show the usage of Stopwatch and not to determine the speed of the website.

Method Stopwatch.Start() starts the timer, and ticks in parallel until the Stopwatch.Stop() is executed. The start method does not start the elapsed time at 0, if executed again. To set the elapsed time to 0 use either Stopwatch.Restart() or call Stopwatch.Reset() before the next start(). So understand that “A Stopwatch instance calculates and retains the cumulative elapsed time across multiple time intervals, until the instance is reset/restarted.” – MSDN. To start a fresh timer use Stopwatch stopwatch = Stopwatch.StartNew() which is a static method.

This comes very handy when you use this for all your performance monitoring or benchmarking on/the your operations.

Cheers!

Categories: C# Tags:

Long time since I blogged!

October 20, 2010 1 comment

No laziness is not the only reason why I skipped blogging for months together. I was on a complete learning mode, when I say learning its not just the technical stuffs I am talking about. I was involved in various activities of life, which demanded a lot of my personal time. A lot of activities which were completely new to me, and had no clue on how to go about it. I am not completely done with it (that said there is no definite time frame on blogging), but have decided to pull out some of my time. My passion for technology and photography are in my breath and that keeps me alive, but off late I realized it wasn’t enough! probably I need some food to survive. And that is something which I will let you know in years to come!

My transition from an entrepreneur to a full time employee was a tough decision, but have to do that to sustain and to overcome the loses! Am I complaining? No I am lucky enough in doing something what I always wanted to do and that is programming! The point is having bitten by the entrepreneur bug in some time of my life, has not let me sleep peaceful. Regaining those momentum and going forward in that direction is even more riskier until I have some concrete things planned.

Other than the learning of life I had couple of new learning which were technical and they were completely new from my field of work. But it was necessary to keep the momentum on. Oh did I say I have some concrete plans? No not yet!

Sometimes taking a right decision is far more easier than making your decision right!

To restart blogging, I am initiating a simple technical post which is coming up next!

Cheers!

Categories: Personal Tags:
Follow

Get every new post delivered to your Inbox.