Undo Enumerable.AsParallel(), going back to serial mode
Asked Answered
A

1

7

Assume you have a LINQ query like

source.AsParallel().Where(expensiveOperation).Select(cheapOperation)

I suppose in this case Select also runs in parallel execution mode. Maybe it's just a cheap operation like i => i*2, so is there a way to stop parallel execution at a point of querying with chained methods?

(maybe like .AsParallel().Where(expensiveOp).AsSerial?().Select(cheapOp)?)

Absorptance answered 23/12, 2013 at 22:35 Comment(1)
I belive PLINQ is smart and executes the Where() together with the Select(). So, even when Select() is cheap, you probably don't need to force it to be sequential. But you'll have to measure that for yourself.Cissoid
M
10

The operation you're looking for is AsSequential.

source.AsParallel().Where(expensiveOp).AsSequential().Select(cheapOp)
Madlynmadman answered 23/12, 2013 at 22:38 Comment(1)
looks like I was wrong on vocabulary haha. Thanks for noting. I literally looked for AsSerial() in docs for a few mins.Absorptance

© 2022 - 2024 — McMap. All rights reserved.