{"id":841,"date":"2023-01-02T10:54:49","date_gmt":"2023-01-02T01:54:49","guid":{"rendered":"https:\/\/csharp.ihavenomoney.co.kr\/?p=841"},"modified":"2023-01-02T10:57:05","modified_gmt":"2023-01-02T01:57:05","slug":"parallel-foreach-%ec%86%8d%eb%8f%84","status":"publish","type":"post","link":"https:\/\/csharp.ihavenomoney.co.kr\/?p=841","title":{"rendered":"Parallel.ForEach \uc18d\ub3c4"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Overview : 2000\ub9cc\uac1c \uc790\uc5f0\uc218\uc911 \uc18c\uc218 \ucc3e\uae30 \uc18d\ub3c4<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">\uc0ac\uc6a9\ubc95 :<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:default decode:true \">using System.Threading.Tasks;\n\n        private static IList&lt;int&gt; GetPrimeListWithParallel(IList&lt;int&gt; numbers)\n        {\n            var primeNumbers = new ConcurrentBag&lt;int&gt;(); \/\/ConcurrentBag\n\n            Parallel.ForEach(numbers, number =&gt;\n            {\n                if (IsPrime(number))\n                {\n                    primeNumbers.Add(number);\n                }\n            });\n\n            return primeNumbers.ToList();\n        }<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Sample :<\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:default decode:true \" >using System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Threading.Tasks;\nusing System.Collections.Concurrent;\nusing System.Diagnostics;\n\nnamespace ConsoleTest\n{\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            \/\/ 2 million\n            int limit = 2_000_000;\n            \/\/int limit = 2000;\n            List&lt;int&gt; numbers = Enumerable.Range(0, limit).ToList();\n\n            Stopwatch watch = Stopwatch.StartNew();\n            IList&lt;int&gt; primeNumbersFromForeach = GetPrimeList(numbers);\n            watch.Stop();\n\n            Stopwatch watchForParallel = Stopwatch.StartNew();\n            IList&lt;int&gt; primeNumbersFromParallelForeach = GetPrimeListWithParallel(numbers);\n            watchForParallel.Stop();\n\n            Console.WriteLine($\"Classical foreach loop |  \uc18c\uc218\ub4e4 : {primeNumbersFromForeach.Count} | \uac78\ub9b0\uc2dc\uac04 : {watch.ElapsedMilliseconds} ms.\");\n            Console.WriteLine($\"Parallel.ForEach loop  | \uc18c\uc218\ub4e4 : {primeNumbersFromParallelForeach.Count} | \uac78\ub9b0\uc2dc\uac04 : {watchForParallel.ElapsedMilliseconds} ms.\");\n\n            \/\/Console.WriteLine($\"Classical foreach loop |  \uc18c\uc218\ub4e4 :\");\n            \/\/primeNumbersFromForeach.Where(numberWrite).ToList();\n            \/\/Console.WriteLine($\"Parallel.ForEach loop |  \uc18c\uc218\ub4e4 :\");\n            \/\/primeNumbersFromParallelForeach.Where(numberWrite).ToList();\n\n            Console.WriteLine(\"Press any key to exit.\");\n            Console.ReadLine();\n        }\n\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ GetPrimeList returns Prime numbers by using sequential ForEach\n        \/\/\/ &lt;\/summary&gt;\n        \/\/\/ &lt;param name=\"inputs\"&gt;&lt;\/param&gt;\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\n        private static IList&lt;int&gt; GetPrimeList(IList&lt;int&gt; numbers) =&gt; numbers.Where(IsPrime).ToList(); \/\/\n\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ GetPrimeListWithParallel returns Prime numbers by using Parallel.ForEach\n        \/\/\/ &lt;\/summary&gt;\n        \/\/\/ &lt;param name=\"numbers\"&gt;&lt;\/param&gt;\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\n        private static IList&lt;int&gt; GetPrimeListWithParallel(IList&lt;int&gt; numbers)\n        {\n            var primeNumbers = new ConcurrentBag&lt;int&gt;(); \/\/ConcurrentBag\n\n            Parallel.ForEach(numbers, number =&gt;\n            {\n                if (IsPrime(number))\n                {\n                    primeNumbers.Add(number);\n                }\n            });\n\n            return primeNumbers.ToList();\n        }\n\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ IsPrime returns true if number is Prime, else false.(https:\/\/en.wikipedia.org\/wiki\/Prime_number)\n        \/\/\/ \uc5b4\ub5a4 \uc790\uc5f0\uc218n\uc774 \uc18c\uc218\uc784\uc744 \ud310\uc815\ud558\uae30 \uc704\ud574\uc120 \ub274\ud2b8n \uae4c\uc9c0\uc758 \uc218 \uc911 1\uc744 \uc81c\uc678\ud558\uace0 \uadf8 \uc790\uc5f0\uc218\uc758 \uc57d\uc218\uac00 \uc788\ub294\uc9c0 \ud655\uc778\ud558\uba74 \ub41c\ub2e4.\n        \/\/\/ &lt;\/summary&gt;\n        \/\/\/ &lt;param name=\"number\"&gt;&lt;\/param&gt;\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\n        private static bool IsPrime(int number)\n        {\n            if (number &lt;= 2)  \/\/0,1,2 \uc81c\uc678\n            {\n                return false;\n            }\n\n            for (var divisor = 2; divisor &lt;= Math.Sqrt(number); divisor++)\n            {\n                \/\/Console.WriteLine($\"number : {number} | divisor : {divisor} | Math.Sqrt(number) : {Math.Sqrt(number)} | number % divisor {number % divisor} \");\n\n                if (number % divisor == 0)\n                {\n                    \/\/\uc57d\uc218 \uc788\ub2e4.\n                    return false;\n                }\n                \/\/\uc57d\uc218 \uc5c6\ub2e4. =&gt;true\n            }\n\n            return true;\n        }\n\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \ub0a8\uc740\uc18c\uc218 \ub098\uc5f4\n        \/\/\/ &lt;\/summary&gt;\n        \/\/\/ &lt;param name=\"number\"&gt;&lt;\/param&gt;\n        private static bool numberWrite(int number)\n        {\n            Console.WriteLine($\"\ub0a8\uc740\uc18c\uc218 : {number}\");\n            return true;\n        }\n\n    }\n}\n<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"theme:vs2012-black lang:default decode:true \">Classical foreach loop |  \uc18c\uc218\ub4e4 : 148932 | \uac78\ub9b0\uc2dc\uac04 : 1257 ms.\nParallel.ForEach loop  | \uc18c\uc218\ub4e4 : 148932 | \uac78\ub9b0\uc2dc\uac04 : 363 ms.\nPress any key to exit.\n<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Overview : 2000\ub9cc\uac1c \uc790\uc5f0\uc218\uc911 \uc18c\uc218 \ucc3e\uae30 \uc18d\ub3c4 \uc0ac\uc6a9\ubc95 : Sample :<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[74],"tags":[],"class_list":["post-841","post","type-post","status-publish","format-standard","hentry","category-c-form"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/841","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=841"}],"version-history":[{"count":3,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/841\/revisions"}],"predecessor-version":[{"id":845,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/841\/revisions\/845"}],"wp:attachment":[{"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/csharp.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}