Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the misunderstandings in the use of DotNet parallel computing

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the misunderstandings in the use of DotNet parallel computing, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Parallel computing or parallel computing is relative to serial computing. The so-called parallel computing can be divided into time parallel and space parallel. Temporal parallelism refers to pipelined technology, while spatial parallelism refers to the concurrent execution of computing with multiple processors.

Parallel computing is undoubtedly a bright spot of the .net Framework platform, which automatically decomposes a task and executes it concurrently. Programmers do not have to worry about the cooperation and synchronization between tasks, which makes it possible to focus more on the implementation of the business.

TPL (Task Parallel Library) in .NET, which means task parallel library in Chinese, is designed to make it easier to write managed code that automatically uses multiprocessors. With this library, users can easily express potential parallelism in existing sequence code, so that parallel tasks exposed in sequence code will run on all available processors at the same time, which usually increases speed greatly.

However, from many examples of parallel computing that have been published on the Internet, there are many misunderstandings or even misleading, which leads to some wrong ideas among front-line programmers. Most of them show the performance advantages of parallel computing through examples. It seems that programmers can improve program performance N times easily, if these ideas are applied to practice without comparison. Then it will cause certain losses. This article is about the rational use of parallel computing for your reference. These misunderstandings mainly include:

1. As long as parallelism is used, program performance will be improved.

two。 The more parallel loops are nested, the higher the program performance is.

3. Parallel computing is a matter of runtime

Let's explain these misunderstandings one by one.

Misunderstanding one. As long as parallelism is used, program performance will be improved.

This is not the case in real-time. In fact, the use of parallel computing requires very strict prerequisites. In general, the extensive use of parallel computing will not improve performance, but will be counterproductive. Here are two Case to explain.

Case 1. Using Thread.Sleep () to compare the performance of parallel and single-line programs is not objective.

In many examples where the performance of parallel computing is compared with that of single-line programs, many of them contain statements like Thread.Sleep (). Running such a Demo we do see that the parallel time results have been improved so much, but have you carefully studied the reasons for the decrease in time?

There are two pieces of code:

Code Part A: for (int I = 0; I

< 10; i++) { a = i.ToString(); Thread.Sleep(200); } Code Part B: Parallel.For(0, 10, (i) =>

{a = i.ToString (); Thread.Sleep;})

On my dual-core computer, the test results were exciting: Code Part A ran for more than 2 seconds, while Code Part B ran for just over 800ms, which greatly reduced the time, but did you decide to move your code to parallel mode?

I suggest you wait and see. The reason why Code Part B has higher performance than An is not because of the performance improvement brought about by the parallelism of the main code, but because of Sleep (). In a parallel environment, the task actually only rests 1 gram N (N is the number of parallelism), not all of the single-line programs, because TPL breaks down the loop work on dual-core computers. Code Part B is equivalent to two five-time loops at the same time, and Sleep () has little consumption of shared resources and does not need to be synchronized with other processes, so the running time is lower than that of 10 cycles. If we remove the Sleep statements in Code Part An and B, what will be the result? The answer is that the two are very close. In fact, in the case of short main code, parallel computing will show a certain degree of performance instability, leave some here, interested friends themselves to test it.

To choose parallel computing to deal with problems, first of all, make sure that your samples or requirements are suitable for parallel processing, such as not using parallel computing when writing sorting algorithms.

Case 2: parallel programs are different in dealing with strings and numbers.

@ string accumulation:

Single line:

For (int I = 0; I

< 100000; i++) { a += i.ToString(); } 并行: Parallel.For(0, 100000, (i) =>

{a + = i.ToString ();})

@ string comparison:

Single line:

For (int I = 0; I

< 100000; i++) { f = a.Equals(i.ToString()); } 并行: Parallel.For(0, 100000, (i) =>

{f = a.Equals (i.ToString ());})

@ numerical comparison:

Single line:

For (int I = 0; I

< 100000000; i++) { f = i == j; } 并行: Parallel.For(0, 100000000, (i) =>

{f = I = = j;})

Running the above three pieces of test code shows that TPL is quite satisfactory for string processing. So it is not appropriate to use TPL library handlers in all cases, which is important for traversal scenarios in the program. When using PLINQ, it is recommended to analyze the type and operation of the sample space before deciding which calculation to use.

Misunderstanding two. The more parallel loops are nested, the higher the program performance is.

For a code segment with two or more layers of loop, before you decide to use parallelism, the first thing to do is to find out where the main consumption of the code is, whether it is on the outer layer or on the inner layer. It is not too late to evaluate that when the performance improvement caused by parallelism is greater than the loss, it is not too late to reconstruct to parallelism, because TPL provides parallelism support in many cases, and many programmers use parallelism where parallelism can be used without testing and comparison. In fact, there are many times when parallelism is added everywhere, and the performance of the program will be lost, so there are no more examples here.

As can be seen from the following figure, although TPL automatically manages the objects in the loop, there is some performance loss in these "automatization". If our code constantly requires TPL to split, merge, and synchronize objects, while ignoring the optimization of the business itself, it will undoubtedly be detrimental to performance.

Here, Aicken gives a suggestion, that is, before parallelism, you need to evaluate how much work this code has to do and whether it is necessary to do so. Is there any competition for disk and other "write" requirements in this code? What tasks does the code deal with, and whether the running environment of the code supports parallelism; in addition, the performance test is carried out after the code is refactored, so as to ensure that parallel computing is meaningful.

In fact, in addition to the performance improvement of parallel computing, there is another valuable thing, that is, the refactoring of the code, concise and structural code, more in line with the requirements of coding progress, isn't it?

Thank you for reading this article carefully. I hope the article "what are the misunderstandings in the use of DotNet parallel computing" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report