Difference linq and plinq

前端 未结 5 655
旧巷少年郎
旧巷少年郎 2020-12-11 04:20

What is the difference between these two?

What is the best way to compare ?

It is always better plinq ?

When we use plinq ?

5条回答
  •  时光取名叫无心
    2020-12-11 04:46

    Linq is a collection of technologies that work together to solve a similar family of problems - in all of them you have a source of data (xml file or files, database contents, collection of objects in memory) and you want to retrieve some or all of this data and act on it in some way. Linq works on the commonality of that set of problems such that:

    var brithdays = from user in users where
      user.dob.Date == DateTime.Today && user.ReceiveMails
      select new{user.Firstname, user.Lastname, user.Email};
    foreach(bdUser in birthdays)
      SendBirthdayMail(bdUser.Firstname, bdUser.Lastname, bdUser.Email);
    

    And the equivalent (explicit use of Linq-related classes and methods with a traditional C# syntax):

    var birthdays = users
      .Where(user => user.dob.Date == DateTime.Today)
      .Select(user => new{user.Firstname, user.Lastname, user.Email});
    foreach(bdUser in birthdays)
      SendBirthdayMail(bdUser.Firstname, bdUser.Lastname, bdUser.Email);
    

    Are both examples of code that could work regardless of whether it's going to be turned into database calls, parsing of xml documents, or a search through an array of objects.

    The only difference is what sort of object users is. If it was a list, array, or other enumerable collection, it would be linq-to-objects, if it was a System.Data.Linq.Table it would be linq to sql. The former would result in in-memory operations, the latter in a SQL query that would then be deserialised to in-memory objects as late as possible.

    If it was a ParallelQuery - produced by calling .AsParallel on an in-memory enumerable collection - then the query will be performed in-memroy, parallelised (most of the time) so as to performed by multiple threads - ideally keeping each core busy moving the work forward.

    Obviously the idea here is to be faster. When it works well, it does.

    There are some downsides though.

    First, there's always some overhead to getting the parallelisation going, even in cases where it ends up not being possible to parallelise. If there isn't enough work being done on data, this overhead will out-weigh any potential gains.

    Second, the benefits of parallel processing depends on the cores available. With a query that doesn't end up blocking on resources on a 4-core machine, you theoretically get a 4-times speed up (4 hyper-threaded might give you more or even less, but probably not 8-times since hyper-threading's doubling of some parts of the CPU doesn't give a clear two-times increase). With the same query on a single-core, or with processor affinity meaning only one core is available (e.g. a webserver in "web-garden" mode), then there's no speed-up. There could still be a gain if there's blocking on resources, but the benefit depends on the machine then.

    Third, if there's any shared resource (maybe an collection results are being output to) is used in a non-threadsafe way, it can go pretty badly wrong with incorrect results, crashes, etc.

    Fourth, if there's a shared resource being used in a threadsafe way, and that threadsafety comes from locking, there could be enough contention to become a bottleneck that undoes all the benefits from the parallelisation.

    Fifth, if you've a four-core machine working on more or less the same algorithm on four different threads (perhaps in a client-server situation due to four clients, or on a desktop situation from a set of similar tasks higher in the process), then they're alreay making the best use of those cores. Splitting the work in the algorithm up so as to be handled across all four cores means you've moved from four threads using one core each to 16 threads fighting over four cores. At best it'll be the same, and likely overheads will make it slightly worse.

    It can still be a major win in a lot of cases, but the above should make it clear that it won't always.

提交回复
热议问题