plinq

AsParallel () and Any()?

强颜欢笑 提交于 2019-12-07 00:22:15
问题 I've seen this code which check a condition using AsParallel() and Any() : bool IsAnyDeviceConnected() { return m_devices.Any(d => d.IsConnected); } and to make it faster : bool IsAnyDeviceConnected() { return m_devices.AsParallel().Any(d => d.IsConnected); } But looking at Any() : internal static bool Any<T>(this IEnumerable<T> source, Func<T, bool> predicate) { foreach (T element in source) { if (predicate(element)) { return true; } } return false; } I don't see ( obviously ) - that it does

Using Parallel Linq Extensions to union two sequences, how can one yield the fastest results first?

给你一囗甜甜゛ 提交于 2019-12-06 21:12:19
问题 Let's say I have two sequences returning integers 1 to 5. The first returns 1, 2 and 3 very fast, but 4 and 5 take 200ms each. public static IEnumerable<int> FastFirst() { for (int i = 1; i < 6; i++) { if (i > 3) Thread.Sleep(200); yield return i; } } The second returns 1, 2 and 3 with a 200ms delay, but 4 and 5 are returned fast. public static IEnumerable<int> SlowFirst() { for (int i = 1; i < 6; i++) { if (i < 4) Thread.Sleep(200); yield return i; } } Unioning both these sequences give me

NHibernate LINQ + PLINQ

▼魔方 西西 提交于 2019-12-06 14:57:01
i've just started reading up on PLINQ and find it fasinating. I'm using NHib->Linq in my projects - does anyone know if there's any benefit/problems using PLINQ type queries with NHLinq? w:// If you're trying to parallelize several NHibernate queries with PLINQ, keep in mind that NHibernate's ISession is not thread-safe. You have to use a new ISession for each step of the PLINQ loop, since each step can potentially run in another thread. If you're trying to use PLINQ constructs within a single NHibernate query at best you'll get an exception since SQL itself does not have any parallelizing

PLINQ on ConcurrentQueue isn't multithreading

别说谁变了你拦得住时间么 提交于 2019-12-06 10:18:54
I have the following PLINQ statement in a C# program: foreach (ArrestRecord arrest in from row in arrestQueue.AsParallel() select row) { Geocoder geocodeThis = new Geocoder(arrest); writeQueue.Enqueue(geocodeThis.Geocode()); Console.Out.WriteLine("Enqueued " + ++k); } Both arrestQueue and writeQueue are ConcurrentQueues . Nothing is running in parallel: While running, total CPU usage is about 30%, and this is with everything else running, too. I have 8 cores (Hyper-Threading on a Core i7 720QM with 4 physical cores), and 4 of the 8 cores have virtually no utilization at all. The rest run

Thread.Sleep blocking parallel execution of tasks

不打扰是莪最后的温柔 提交于 2019-12-06 05:57:47
问题 I'm calling a worker method that calls to the database that then iterates and yield returns values for parallel processing. To prevent it from hammering the database, I have a Thread.Sleep in there to pause the execution to the DB. However, this appears to be blocking executions that are still occurring in the Parallel.ForEach. What is the best way to achieve this to prevent blocking? private void ProcessWorkItems() { _cancellation = new CancellationTokenSource(); _cancellation.Token.Register

is it ok to use plinq ForAll for a bulk insert into database?

元气小坏坏 提交于 2019-12-06 03:10:00
问题 I'm doing like this: entities.AsParallel().ForAll(o => repository.Insert(o)); is this good, am I going to have more performance with this ? 回答1: No. This one can be faster, as it leverages the paralellism to the SQL, but in the end the SQL has to make a lock for the table (page), as it makes an insert. therefore each paralell request is executed after another again. If you want to make a bulk insert, than make a SP accepting all entries (e.g. a table with SQL 2008.) or do it with Linq2SQL.

How does AsParallel() split it's 'source'?

谁都会走 提交于 2019-12-06 02:31:28
问题 I'm trying to determine how AsParallel() splits it's 'source', and indeed what is meant by 'source'... For example... public class CSVItem { public DateTime Date { get; set; } public string AccountNumber { get; set; } } List<CSVItem> CSVItemList = new List<CSVItem>(); Then put 500k varying CSVItem's into CSVItemList. Then use: CSVItemList = CSVItemList.AsParallel().OrderBy(x => x.AccountNumber).ThenBy(q => q.Date).ToList(); Will it only split the 'source' (meaning for example 250k records

PLINQ query giving overflow exception

百般思念 提交于 2019-12-06 01:08:07
问题 I'm running a PLINQ query as follows: ParallelQuery<string> winningCombos = from n in nextComboMaker.GetNextCombo() .AsParallel().WithCancellation(_cancelSource.Token) where ComboWasAWinner(n) select n; ConcurrentBag<string> wins = new ConcurrentBag<string>(); foreach (var winningCombo in winningCombos) { wins.Add(winningCombo); if (wins.Count == _maxWinsAllowed) break; } The GetNextCombo method is just returning the next combination of letters and numbers, with possibilities up to the

Can we add new elements to a list using Parallel.ForEach()?

半腔热情 提交于 2019-12-05 17:12:28
My code does very simple stuff list already has elements. I have approximately 25000 elements (and I'm expecting to have more) in the list and each element is small (DateTime). List<DateTime> newList = new List<DateTime>(); Parallel.ForEach(list, l => newlist.Add(new DateTime(l.Ticks + 5000))); i.e, based on each element, I'm creating new elements and adding them to a different list. But, this doesn't seem to be a good programming approach. I hit this exceptions some times, but not everytime. IndexOutOfRangeException : {"Index was outside the bounds of the array."} Can we add elements to a

Make C# ParallelEnumerable.OrderBy stable sort

怎甘沉沦 提交于 2019-12-05 16:55:28
I'm sorting a list of objects by their integer ids in parallel using OrderBy . I have a few objects with the same id and need the sort to be stable. According to Microsoft's documentation , the parallelized OrderBy is not stable, but there is an implementation approach to make it stable. However, I cannot find an example of this. var list = new List<pair>() { new pair("a", 1), new pair("b", 1), new pair("c", 2), new pair("d", 3), new pair("e", 4) }; var newList = list.AsParallel().WithDegreeOfParallelism(4).OrderBy<pair, int>(p => p.order); private class pair { private String name; public int