Wednesday, 28 March 2012

C# Stopwatch Class

You can get a value of elapsed time using the properties or the methods of "Stopwatch Class". I used it to measure the elapsed time of search function.

http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx
------------------------------------------------------------------------------------------------
public static void SearchName(ArrayList nameList)
        {
            string nameToFind;
            Console.Write("Enter the name to find : ");
            nameToFind = Console.ReadLine();
            Console.WriteLine("\nStarting manual search\n");
            bool found = false;
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            for (int i = 0; i < nameList.Count; i++)
            {
                if (nameList[i].ToString().Equals(nameToFind, StringComparison.OrdinalIgnoreCase))
                {
                    found = true;
                    stopwatch.Stop();
                    break;
                }
            }
            stopwatch.Stop();

            if (!found)
            {
                Console.WriteLine("Sorry the name " + nameToFind + " could not be found,\n");
                Console.WriteLine("The search took {0} Ticks", stopwatch.ElapsedTicks);
            }
            else
            {
                Console.WriteLine("The name " + nameToFind + " was found");
                Console.WriteLine("The search took {0} Ticks", stopwatch.ElapsedTicks);
            }
        }
------------------------------------------------------------------------------------------------

No comments:

Post a Comment