What are the advantages of parallel.ForEach in C #?

4

Working with C # I have seen that we have the option to work with parallel.ForEach() . What is the advantage of working with it and not foreach ?

    
asked by anonymous 14.05.2016 / 03:36

2 answers

2

Depends on the application.

If you have lots of data to process and the order does not import the best is parallel processing (Parallel.ForEach ()), otherwise the best is the foreach or another loop.

Parallel.ForEach (), what it does is pick up your collection and split into threads and execute whatever has to be done! basically this is it.

    
14.05.2016 / 04:11
6

Her name pretty much delivers everything that is being asked. In this way the iteration in a sequence of items is evaluated in parallel, as far as possible, and in some scenarios this can take advantage of all the potential of the hardware being used. With parallelization the total process can end faster. It can be applied to any object that implements the interface IEnumerable (see more ).

Parallelization will not always bring gains . There is a certain risk of making an algorithm that does not work so well in parallel. It is always necessary to test whether or not to compensate.

There are problems that can not be broken into parts to parallelize. The most typical example is when a result depends on the previous results.

Documentation .

    
14.05.2016 / 03:49