|
|
@ -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<string> textList = new List<string>(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<string> textList = new List<string>(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(); }); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |