Part 97 Performance of a multithreaded program

class Program
    {
        static void Main(string[] args)
        {
            Stopwatch s = new Stopwatch();
            s.Start();
            EvenNumbersSum();
            OddNumbersSum();
            s.Stop();
            Console.WriteLine("before using multiple threads"+s.ElapsedMilliseconds);
            s = new Stopwatch();
            s.Start();
            Thread t1 = new Thread(EvenNumbersSum);
            Thread t2 = new Thread(OddNumbersSum);
            t1.Start();
            t2.Start();
            t1.Join();
            t2.Join();
            s.Stop();
            Console.WriteLine("after using multiple threads"+s.ElapsedMilliseconds);

        }

        public static void EvenNumbersSum()
        {
            double sum=0;
            for(int i=0;i<=50000000;i++)
            {
                if(i%2==0)
                {
                    sum += i;
                }
            }
            Console.WriteLine("sum= "+sum);
        }

        public static void OddNumbersSum()
        {
            double sum = 0;
            for (int i = 0; i <= 50000000; i++)
            {
                if (i % 2 == 1)
                {
                    sum += i;
                }
            }
            Console.WriteLine("sum= " + sum);
        }
    }
View Code

posted @ 2015-10-11 23:51  Gester.Lin  阅读(151)  评论(0编辑  收藏  举报