-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
184 lines (138 loc) · 5.1 KB
/
Copy pathProgram.cs
File metadata and controls
184 lines (138 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Parallel线程
{
internal class Program
{
static void Main(string[] args)
{
//调用线程个数不同
int[] data = new int[20];
for (int i = 0; i < data.Length; i++)
{
data[i] = i;
}
Console.WriteLine("普通 for 循环:");
for (int i = 0; i < data.Length; i++)
{
Console.WriteLine($"for: 索引={i}, 线程ID={Thread.CurrentThread.ManagedThreadId}");
}
Console.WriteLine("\nParallel.For 并行循环:");
Parallel.For(0, data.Length, i =>
{
Console.WriteLine($"Parallel.For: 索引={i}, 线程ID={Thread.CurrentThread.ManagedThreadId}");
});
Console.WriteLine("-----------------------------");
Int32 dataSize = 100000000;
Int32[] data1 = new Int32[dataSize];
for (Int32 i = 0; i < data1.Length; i++)
{
data1[i] = i;
}
// for循环计时
Stopwatch sw = new Stopwatch();
sw.Start();
for (Int32 i = 0; i < data1.Length; i++)
{
data1[i] = data1[i] * 2;
}
sw.Stop();
Console.WriteLine($"for循环耗时: {sw.ElapsedMilliseconds} ms");
// 还原数据
for (Int32 i = 0; i < data1.Length; i++)
{
data1[i] = i;
}
// Parallel.For计时
sw.Restart();
Parallel.For(0, data1.Length, i =>
{
data1[i] = data1[i] * 2;
});
sw.Stop();
Console.WriteLine($"Parallel.For耗时: {sw.ElapsedMilliseconds} ms");
Console.WriteLine("-----------------------------");
int dataSize10 = 1000;
int[] data10 = new int[dataSize10];
for (int i = 0; i < data10.Length; i++)
{
data10[i] = i;
}
// 普通LINQ
Stopwatch sw1 = new Stopwatch();
sw1.Start();
var linqResult = data10.Where(x => x % 4 == 0).Select(x => ComplexCalculation(x)).ToList();
sw1.Stop();
Console.WriteLine($"普通LINQ耗时: {sw1.ElapsedMilliseconds} ms");
// PLINQ
sw1.Restart();
var plinqResult = data10.AsParallel().Where(x => x % 4 == 0).Select(x => ComplexCalculation(x)).ToList();
sw1.Stop();
Console.WriteLine($"PLINQ耗时: {sw1.ElapsedMilliseconds} ms");
Console.WriteLine("-----------同步线程-异步线程------------------");
Console.WriteLine("同步处理:");
sw.Restart();
for (int i = 0; i < 5; i++)
{
DoWork(i);
}
sw.Stop();
Console.WriteLine($"同步总耗时: {sw.ElapsedMilliseconds} ms");
Console.WriteLine("\n异步并发处理:");
sw.Restart();
Task[] tasks = new Task[5];
for (int i = 0; i < 5; i++)
{
int idx = i;
tasks[i] = Task.Run(() => DoWork(idx));
}
Task.WaitAll(tasks);
sw.Stop();
Console.WriteLine($"异步总耗时: {sw.ElapsedMilliseconds} ms");
Console.WriteLine("\n异步方法+await示例:");
sw.Restart();
RunAsyncDemo().Wait();
sw.Stop();
Console.WriteLine($"async/await总耗时: {sw.ElapsedMilliseconds} ms");
Console.WriteLine("\n按任意键退出...");
Console.ReadKey();
}
static void DoWork(int i)
{
Console.WriteLine($"任务{i}开始,线程ID={Thread.CurrentThread.ManagedThreadId}");
Thread.Sleep(1000); // 模拟耗时操作
Console.WriteLine($"任务{i}结束,线程ID={Thread.CurrentThread.ManagedThreadId}");
}
static async Task RunAsyncDemo()
{
Task[] tasks = new Task[5];
for (int i = 0; i < 5; i++)
{
int idx = i;
tasks[i] = Task.Run(async () =>
{
Console.WriteLine($"async任务{idx}开始,线程ID={Thread.CurrentThread.ManagedThreadId}");
await Task.Delay(1000); // 异步等待
Console.WriteLine($"async任务{idx}结束,线程ID={Thread.CurrentThread.ManagedThreadId}");
});
}
await Task.WhenAll(tasks);
}
// 复杂计算方法
static double ComplexCalculation(int x)
{
double result = x;
for (int i = 0; i < 100; i++)
{
result = Math.Sqrt(result * 1.3501 + 3.141556711);
//Thread.Sleep(5);
}
return result;
}
}
}