Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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:</p> <pre><code>var brithdays = from user in users where user.dob.Date == DateTime.Today &amp;&amp; user.ReceiveMails select new{user.Firstname, user.Lastname, user.Email}; foreach(bdUser in birthdays) SendBirthdayMail(bdUser.Firstname, bdUser.Lastname, bdUser.Email); </code></pre> <p>And the equivalent (explicit use of Linq-related classes and methods with a traditional C# syntax):</p> <pre><code>var birthdays = users .Where(user =&gt; user.dob.Date == DateTime.Today) .Select(user =&gt; new{user.Firstname, user.Lastname, user.Email}); foreach(bdUser in birthdays) SendBirthdayMail(bdUser.Firstname, bdUser.Lastname, bdUser.Email); </code></pre> <p>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.</p> <p>The only difference is what sort of object <code>users</code> is. If it was a list, array, or other enumerable collection, it would be linq-to-objects, if it was a <code>System.Data.Linq.Table</code> 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.</p> <p>If it was a <code>ParallelQuery</code> - produced by calling <code>.AsParallel</code> 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.</p> <p>Obviously the idea here is to be faster. When it works well, it does.</p> <p>There are some downsides though.</p> <p>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.</p> <p>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.</p> <p>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.</p> <p>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.</p> <p>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.</p> <p>It <strong>can</strong> still be a major win in a lot of cases, but the above should make it clear that it won't always.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload