plinq

Running a simple LINQ query in parallel

青春壹個敷衍的年華 提交于 2019-12-21 07:12:33
问题 I'm still very new to LINQ and PLINQ. I generally just use loops and List.BinarySearch in a lot of cases, but I'm trying to get out of that mindset where I can. public class Staff { // ... public bool Matches(string searchString) { // ... } } Using "normal" LINQ - sorry, I'm unfamiliar with the terminology - I can do the following: var matchedStaff = from s in allStaff where s.Matches(searchString) select s; But I'd like to do this in parallel: var matchedStaff = allStaff.AsParallel().Select

Speculative execution using the TPL

拜拜、爱过 提交于 2019-12-19 03:42:39
问题 I have a List<Task<bool>> that I want to enumerate in parallel finding the first task to complete with a result of true and not waiting for or observe exceptions on any of the other tasks still pending. var tasks = new List<Task<bool>> { Task.Delay(2000).ContinueWith(x => false), Task.Delay(0).ContinueWith(x => true), }; I have tried to use PLINQ to do something like: var task = tasks.AsParallel().FirstOrDefault(t => t.Result); Which executes in parallel, but doesn't return as soon as it

How exactly does AsParallel work?

吃可爱长大的小学妹 提交于 2019-12-18 16:53:51
问题 It doesn't seem to do squat for the following test program. Is this because I'm testing with a small list? static void Main(string[] args) { List<int> list = 0.UpTo(4); Test(list.AsParallel()); Test(list); } private static void Test(IEnumerable<int> input) { var timer = new Stopwatch(); timer.Start(); var size = input.Count(); if (input.Where(IsOdd).Count() != size / 2) throw new Exception("Failed to count the odds"); timer.Stop(); Console.WriteLine("Tested " + size + " numbers in " + timer

Exactly what is PLINQ?

╄→гoц情女王★ 提交于 2019-12-18 10:32:27
问题 PLINQ was added in the .NET 4.0 Framework as an extension to LINQ. What is it? What problems does it solve? When is it appropriate and when not? 回答1: This is Parallel LINQ. It's a way to run LINQ queries in parallel on multi-core/multi-processor systems, in order to (hopefully) speed them up. There is a good article on this in MSDN Magazine. For current details and plans, I recommend reading articles on the Parallel Programming with .NET Team Blog. They are the team implementing the parallel

PLINQ Performs Worse Than Usual LINQ

不羁的心 提交于 2019-12-17 18:38:14
问题 Amazingly, using PLINQ did not yield benefits on a small test case I created; in fact, it was even worse than usual LINQ. Here's the test code: int repeatedCount = 10000000; private void button1_Click(object sender, EventArgs e) { var currTime = DateTime.Now; var strList = Enumerable.Repeat(10, repeatedCount); var result = strList.AsParallel().Sum(); var currTime2 = DateTime.Now; textBox1.Text = (currTime2.Ticks-currTime.Ticks).ToString(); } private void button2_Click(object sender, EventArgs

PLINQ: how to run a ParallelQuery on more than 4 threads?

淺唱寂寞╮ 提交于 2019-12-12 11:15:36
问题 Update - changed the title of the question to reflect what I'm really after Consider the following piece of code: // this query generates 12 instances of Func<int>, which each when executed // print something to the console and wait for 1 second. var actions = Enumerable.Range(0, 12).Select(i => new Func<int>(() => { Console.WriteLine("{0} - waiting 1 sec", i); Thread.Sleep(1000); return 1; })); // define a parallel query. Note the WithDegreeOfParallelism call here. var query = from action in

What's the next big thing after LINQ? [closed]

倾然丶 夕夏残阳落幕 提交于 2019-12-12 08:01:15
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 9 years ago . I started using LINQ (Language Integrated Query) when it was still in beta, more specifically Microsoft .NET LINQ Preview (May 2006).

Parallel I/O and Retry Logic for Error Handling

旧巷老猫 提交于 2019-12-11 12:46:33
问题 Normally parallel processing is relevant only to CPU intensive operations. However, PLINQ specifically provides IO-intensive support using the WithDegreeOfParallelism extension. For example: from site in new[] { "www.albahari.com", "www.linqpad.net", "www.oreilly.com", "www.takeonit.com", "stackoverflow.com", "www.rebeccarey.com" } .AsParallel().WithDegreeOfParallelism(6) let p = new Ping().Send (site) select new { site, Result = p.Status, Time = p.RoundtripTime } But if supporting IO is the

Does PLINQ respect SynchronizationContext?

白昼怎懂夜的黑 提交于 2019-12-11 11:28:17
问题 Say, I have the following code: IPrincipal capturedPrincipal = Thread.CurrentPrincipal; myseq.AsParallel().Select(x => { Thread.CurrenctPrincipal = capturedPrincipal; /*call code protected with CAS*/ }); to be sure Thread.CurrenctPrincipal will be propagated to every thread where Select 's delegate will be executed on. I've realized that if I had proper SynchronizationContext set up this would happen automatically. So does PLINQ use SynchronizationContext when queuing work items on ThreadPool

Parallel Linq to Object collection

走远了吗. 提交于 2019-12-11 07:14:38
问题 I got a basic question when I tried with Plinq (Parallel linq ) to object collection and I observed that Plinq Vs normal operation does not have much difference in terms of execution time. Could anybody can check my code and advice me why so happening. I have run this code in i7 processor. class Program { static void Main(string[] args) { new Program().Plinq(); new Program().linq(); Console.ReadLine(); } void Plinq() { DateTime startTime = DateTime.Now; var query1 = (from port in new