From 02d05f4b0b88b93c613b4159584e10d070b7e093 Mon Sep 17 00:00:00 2001 From: syneffort Date: Fri, 11 Nov 2022 13:02:41 +0900 Subject: [PATCH] parallel --- MultiThreadingStudy/MultiThreadingStudy.sln | 6 + MultiThreadingStudy/UseParallel/App.config | 6 + .../UseParallel/DataParallelism.cs | 105 ++++++++++++++++++ MultiThreadingStudy/UseParallel/Program.cs | 26 +++++ .../UseParallel/Properties/AssemblyInfo.cs | 36 ++++++ .../UseParallel/UseParallel.csproj | 54 +++++++++ 6 files changed, 233 insertions(+) create mode 100644 MultiThreadingStudy/UseParallel/App.config create mode 100644 MultiThreadingStudy/UseParallel/DataParallelism.cs create mode 100644 MultiThreadingStudy/UseParallel/Program.cs create mode 100644 MultiThreadingStudy/UseParallel/Properties/AssemblyInfo.cs create mode 100644 MultiThreadingStudy/UseParallel/UseParallel.csproj diff --git a/MultiThreadingStudy/MultiThreadingStudy.sln b/MultiThreadingStudy/MultiThreadingStudy.sln index 589af68..aec9014 100644 --- a/MultiThreadingStudy/MultiThreadingStudy.sln +++ b/MultiThreadingStudy/MultiThreadingStudy.sln @@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UseTask", "UseTask\UseTask. EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CancelTask", "CacelTask\CancelTask.csproj", "{E06CEFBE-F024-4881-951A-D0E80ADB2D22}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UseParallel", "UseParallel\UseParallel.csproj", "{BFA56F90-D861-4F4A-BF1B-A96D54F48AA0}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +41,10 @@ Global {E06CEFBE-F024-4881-951A-D0E80ADB2D22}.Debug|Any CPU.Build.0 = Debug|Any CPU {E06CEFBE-F024-4881-951A-D0E80ADB2D22}.Release|Any CPU.ActiveCfg = Release|Any CPU {E06CEFBE-F024-4881-951A-D0E80ADB2D22}.Release|Any CPU.Build.0 = Release|Any CPU + {BFA56F90-D861-4F4A-BF1B-A96D54F48AA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BFA56F90-D861-4F4A-BF1B-A96D54F48AA0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BFA56F90-D861-4F4A-BF1B-A96D54F48AA0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BFA56F90-D861-4F4A-BF1B-A96D54F48AA0}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/MultiThreadingStudy/UseParallel/App.config b/MultiThreadingStudy/UseParallel/App.config new file mode 100644 index 0000000..aee9adf --- /dev/null +++ b/MultiThreadingStudy/UseParallel/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/MultiThreadingStudy/UseParallel/DataParallelism.cs b/MultiThreadingStudy/UseParallel/DataParallelism.cs new file mode 100644 index 0000000..7a37660 --- /dev/null +++ b/MultiThreadingStudy/UseParallel/DataParallelism.cs @@ -0,0 +1,105 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace UseParallel +{ + internal class DataParallelism + { + public static void Test() + { + for (int i = 0; i < 1000; i++) + { + Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}: {i}"); + } + + Console.Read(); + + Parallel.For(0, 1000, (i) => + { + Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}: {i}"); + + }); + } + + private static readonly int MAX = 10000000; + private static readonly int SHIFT = 3; + + public static void SequentialEncrypt() + { + string text = "I am a boy. My name is Tom"; + List textList = new List(MAX); + for (int i = 0; i < MAX; i++) + { + textList.Add(text); + } + + Stopwatch watch = new Stopwatch(); + watch.Start(); + + for (int i = 0; i < MAX; i++) + { + char[] charArr = textList[i].ToCharArray(); + for (int x = 0; x < charArr.Length; x++) + { + if (charArr[x] >= 'a' && charArr[x] <= 'z') + charArr[x] = (char)('a' + ((charArr[x] - 'a' + SHIFT) % 26)); + else if (charArr[x] >= 'A' && charArr[x] <= 'Z') + charArr[x] = (char)('A' + ((charArr[x] - 'A' + SHIFT) % 26)); + } + + textList[i] = new string(charArr); + } + + watch.Stop(); + + Console.WriteLine($"Elapsed: {watch.Elapsed.ToString()}"); + + } + + public static void ParallelEncrypt() + { + string text = "I am a boy. My name is Tom"; + List textList = new List(MAX); + for (int i = 0; i < MAX; i++) + { + textList.Add(text); + } + + Stopwatch watch = new Stopwatch(); + watch.Start(); + + Parallel.For(0, MAX, i => + { + char[] charArr = textList[i].ToCharArray(); + for (int x = 0; x < charArr.Length; x++) + { + if (charArr[x] >= 'a' && charArr[x] <= 'z') + charArr[x] = (char)('a' + ((charArr[x] - 'a' + SHIFT) % 26)); + else if (charArr[x] >= 'A' && charArr[x] <= 'Z') + charArr[x] = (char)('A' + ((charArr[x] - 'A' + SHIFT) % 26)); + } + + textList[i] = new string(charArr); + }); + + watch.Stop(); + + Console.WriteLine($"Elapsed: {watch.Elapsed.ToString()}"); + } + + public static void MultiInvoke() + { + Parallel.Invoke( + () => { ParallelEncrypt(); }, + () => { ParallelEncrypt(); }, + () => { ParallelEncrypt(); }, + () => { ParallelEncrypt(); }, + () => { ParallelEncrypt(); }); + } + } +} diff --git a/MultiThreadingStudy/UseParallel/Program.cs b/MultiThreadingStudy/UseParallel/Program.cs new file mode 100644 index 0000000..570e721 --- /dev/null +++ b/MultiThreadingStudy/UseParallel/Program.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace UseParallel +{ + internal class Program + { + static void Main(string[] args) + { + //DataParallelism.Test(); + + //DataParallelism.SequentialEncrypt(); + + //Console.ReadKey(); + + //DataParallelism.ParallelEncrypt(); + + DataParallelism.MultiInvoke(); + + Console.ReadKey(); + } + } +} diff --git a/MultiThreadingStudy/UseParallel/Properties/AssemblyInfo.cs b/MultiThreadingStudy/UseParallel/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..dd02652 --- /dev/null +++ b/MultiThreadingStudy/UseParallel/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 어셈블리에 대한 일반 정보는 다음 특성 집합을 통해 +// 제어됩니다. 어셈블리와 관련된 정보를 수정하려면 +// 이러한 특성 값을 변경하세요. +[assembly: AssemblyTitle("UseParallel")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("UseParallel")] +[assembly: AssemblyCopyright("Copyright © 2022")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// ComVisible을 false로 설정하면 이 어셈블리의 형식이 COM 구성 요소에 +// 표시되지 않습니다. COM에서 이 어셈블리의 형식에 액세스하려면 +// 해당 형식에 대해 ComVisible 특성을 true로 설정하세요. +[assembly: ComVisible(false)] + +// 이 프로젝트가 COM에 노출되는 경우 다음 GUID는 typelib의 ID를 나타냅니다. +[assembly: Guid("bfa56f90-d861-4f4a-bf1b-a96d54f48aa0")] + +// 어셈블리의 버전 정보는 다음 네 가지 값으로 구성됩니다. +// +// 주 버전 +// 부 버전 +// 빌드 번호 +// 수정 버전 +// +// 모든 값을 지정하거나 아래와 같이 '*'를 사용하여 빌드 번호 및 수정 번호를 +// 기본값으로 할 수 있습니다. +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/MultiThreadingStudy/UseParallel/UseParallel.csproj b/MultiThreadingStudy/UseParallel/UseParallel.csproj new file mode 100644 index 0000000..dc32c47 --- /dev/null +++ b/MultiThreadingStudy/UseParallel/UseParallel.csproj @@ -0,0 +1,54 @@ + + + + + Debug + AnyCPU + {BFA56F90-D861-4F4A-BF1B-A96D54F48AA0} + Exe + UseParallel + UseParallel + v4.8.1 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file