본문 바로가기
C#/책 예제

C# Task Parallel 클래스 사용하기.

by HyunS_ 2019. 6. 8.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace ThreadNTask
{
    class Program
    {
        static void Main(string[] args)
        {
            long from = Convert.ToInt64(args[0]);
            long to   = Convert.ToInt64(args[1]);

            Console.WriteLine("Please press enter to start");
            Console.ReadLine();
            Console.WriteLine("Started");

            DateTime startTime = DateTime.Now;
            List<long> total = new List<long>();

            Parallel.For(from, to, (long i) => 
            {
                if(IsPrime(i))
                { 
                    total.Add(i);
                }
            });

            DateTime endTime = DateTime.Now;

            TimeSpan ellapsed = endTime - startTime;

            Console.WriteLine("Prime number count between {0} and {1}: {2}", from, to, total.Count);
            Console.WriteLine("Ellapsed time : {0}", ellapsed);
            
        }

        static bool IsPrime(long number)
        { 
            if(number < 2)
            { 
                return false;
            }

            if(number % 2 == 0 && number != 2)
            { 
                return false;
            }

            for (long i = 2; i < number; i++)
            {
                if(number % i == 0)
                { 
                    return false;
                }
            }

            return true;
        }
    }
}
728x90

댓글