优化这段代码: var dic = new ConcurrentDictionary<int, int>(); int i = 0; var t1 = Task.Run(() => { Parallel.For(0, 10000, t => { dic.AddOrUpdate(1, t, (k, v) => { i++; return t; }); }); });
时间: 2023-11-20 11:06:22 浏览: 40
可以将Parallel.For改为Parallel.ForEach,并使用局部变量来代替i计数,以避免线程安全问题。同时可以将ConcurrentDictionary改为普通的Dictionary,因为并发字典会对性能有一定的影响。
重构后的代码如下:
```
var dic = new Dictionary<int, int>();
int count = 0;
Parallel.ForEach(Enumerable.Range(0, 10000), () => 0, (i, state, localCount) =>
{
dic.AddOrUpdate(1, i, (k, v) => { localCount++; return i; });
return localCount;
}, (x) => Interlocked.Add(ref count, x));
```
这里使用了Parallel.ForEach的4个参数的重载形式,第一个参数指定迭代范围,第二个参数是初始化本地状态的函数,在本例中不需要,返回值为0即可。第三个参数是每个迭代项执行的操作,其中localCount是每个线程本地的计数器。第四个参数是合并本地状态的函数,用来将所有线程的计数器累加到count变量中。最后使用Interlocked.Add来保证线程安全地累加计数器。
相关问题
优化代码 Task<int> task1 = Task.Run(() => 1); Task<string> task2 = Task.Run(() => "hello"); await Task.WhenAll(task1, task2); var task1Result = await task1 var task2Result = await task2
在这段代码中,可以通过使用 `ValueTask` 来避免不必要的上下文切换和任务状态的捕获,从而提高代码的性能和效率。改进后的代码如下:
```
ValueTask<int> task1 = Task.Run(() => 1);
ValueTask<string> task2 = Task.Run(() => "hello");
await Task.WhenAll(task1.AsTask(), task2.AsTask());
int task1Result = await task1;
string task2Result = await task2;
```
这里使用 `ValueTask.AsTask()` 将 `ValueTask` 对象转换为 `Task` 对象,以便在 `Task.WhenAll()` 中使用。同时,由于 `ValueTask` 对象可以直接获取结果,因此不需要使用 `await` 关键字获取结果,而是直接使用变量获取即可。这样可以避免不必要的上下文切换和任务状态的捕获,提高代码的性能和效率。
List<int> aryList1 = new List<int>(); aryList1.AddRange(new int[] { 85, 92, 67, 94, 94 }); List<int> aryList2 = new List<int>(); aryList2.AddRange(new int[] { 50, 60, 57, 95 }); List<int> aryList3 = new List<int>(); aryList3.AddRange(new int[] { 95 }); List<List<int>> classGrade = new List<List<int>>(); classGrade.Add(aryList1); classGrade.Add(aryList2); classGrade.Add(aryList3); List<int> highestGrade(List<List<int>> p2) { List<int> result = new List<int>(); for (int i = 0; i < p2.Count; i++) { int max; List<int> temp = p2[i]; for (int j = 1; j < temp.Count; j++) { if (temp[0] < temp[j]) { max = temp[j]; temp[j] = temp[0]; temp[0] = max; } } result.Add(temp[0]); } return result; }; Console.WriteLine($"The highest grade is {highestGrade(classGrade).Max()}"); 改进代码
可以将内部循环改为使用 LINQ 的 Max() 方法来查找列表中的最大值,而不必手动进行比较和交换。同时,可以使用 var 关键字简化代码。改进后的代码如下:
```
List<int> highestGrade(List<List<int>> p2)
{
List<int> result = new List<int>();
foreach (var temp in p2)
{
result.Add(temp.Max());
}
return result;
}
List<List<int>> classGrade = new List<List<int>>
{
new List<int> { 85, 92, 67, 94, 94 },
new List<int> { 50, 60, 57, 95 },
new List<int> { 95 }
};
Console.WriteLine($"The highest grade is {highestGrade(classGrade).Max()}");
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)