17 October 2013

[C#]จับเวลาการทำงานของโปรแกรม

[ย้ายบทความจาก Gushared.com]
    "เวลา" สิ่งเล็กๆที่บางครั้งเราอาจจะมองข้ามไป เวลาเขียนโปรแกรมแอดมินเองก็ไม่ค่อยจะสนใจหรอกครับว่าโปรแกรมมันจะใช้เวลาทำงานเท่าไหร่ จนเมื่อมาทำงานเนี่ยแหละครับ Requirement ต้องการให้โปรแกรมต้องทำงานเสร็จภายใน 2 นาที *o* ว๊ากกกก จะทำไงเนี่ย! ไอ้โปรแกรมเราก็คำนวณเยอะซะเหลือเกิน สุดท้ายแล้วแอดมินก็ต้องทำจับเวลาดูทีละส่วน ว่าส่วนไหนมันใช้เวลาเยอะเป็นพิเศษ แล้วค่อยปรับอัลกอริทึมใหม่ แต่ประเด็นคือ เราจะจับเวลาเป็นส่วนๆ ยังไงหล่ะ ? เนี่ยแหละครับที่แอดมินอยากบอก
    Stopwatch หลายคนอาจจะรู้จักแล้ว หลายคนไม่เคยคิดจะใช้มัน Stopwatch เป็นคลาสที่ติดมากับ .NET Framework อยู่แล้วเหลือแค่รอให้เราเรียกใช้มัน โดยหลักๆแล้ว เราจะใช้แค่เริ่มนับเวลา หยุด แล้วก็ดูเวลาที่นับได้ แอดมินก็จะแสดงตัวอย่างแค่นี้แหละ หึหึ
using System.Diagnostics;   // อย่าลืม !
Stopwatch sw = new Stopwatch();  //ทำการเรียกใช้คลาส Stopwatch 
sw.Start();   //เริ่มนับเวลา
sw.Stop();    //หยุดนับเวลา
sw.Elapsed;   //เรียกดูค่าเวลาที่นับ รูปแบบ TimeSpan(Hours:Minutes:Seconds.Milliseconds)

    ตัวอย่างโปรแกรม เปรียบเทียบประสิทธิภาพของ Insertion Sort กับ Selection Sort ปล.Console Application นะจ๊ะ
โปรแกรมจะประกอบด้วย 4 เมธอด คือ
//1.Main
//เมธอดทำงานหลัก สำหรับเรียกใช้เมธอดอื่น
        static void Main(string[] args)
        {
            for (int i = 0; i < 20; i++)
            {
                int[] arr = RandomArray();
                Console.Write("Insertion Sort : " + InsertionSort(arr));
                Console.Write(" Selection Sort : " + SelectionSort(arr));
                Console.WriteLine();
            }

            Console.Read();
        }
//2.RandomArray
//เมธอดสำหรับสุ่มค่าตั้งแต่ 0 ถึง 100 ใส่ในอาเรย์ขนาด 100 
//เพื่อใช้สำหรับทดสอบการเรียงค่าในอาเรย์ทั้ง 2 แบบ
        static int[] RandomArray()
        {
            int[] arr = new int[100];

            for (int i = 0; i < arr.Length; i++)
            {
                Random ran = new Random();
                arr[i] = ran.Next(0, 100);
                Thread.Sleep(10);
            }

            return arr;
        }
//3.InsertionSort
// เมธอดการเรียงข้อมูลแบบ Insertion Sort
        static TimeSpan InsertionSort(int[] arr)
        {
            int j;
            int index;
            Stopwatch SW = new Stopwatch();
            SW.Start();
            for (int i = 1; i < arr.Length; i++)
            {
                index = arr[i];
                j = i;

                while ((j > 0) && (arr[j - 1] > index))
                {
                    arr[j] = arr[j - 1];
                    j = j - 1;
                }

                arr[j] = index;
            }
            SW.Stop();
            return SW.Elapsed;
        }
//4.SelectionSort
// เมธอดการเรียงข้อมูลแบบ Selection Sort
        static TimeSpan SelectionSort(int[] arr)
        {
            int min, temp;
            Stopwatch SW = new Stopwatch();
            SW.Start();
            for (int i = 0; i < arr.Length - 1; i++)
            {
                min = i;

                for (int j = i + 1; j < arr.Length; j++)
                {
                    if (arr[j] < arr[min])
                    {
                        min = j;
                    }
                }

                temp = arr[i];
                arr[i] = arr[min];
                arr[min] = temp;
            }
            SW.Stop();
            return SW.Elapsed;
        }
ผลลัพธ์ของโปรแกรม
    จากผลที่ได้จะเห็นว่า Insertion sort ทำเวลาได้ดีกว่า Selection sort จากการจับเวลาด้วย Stopwatch
ศึกษาตัวอย่างเพิ่มเติมได้ที่ dotnetperls.com