.net - Why is code launched on threadpool (via Task.Run) slower than the same code run in BackgroundWorker? -


i have several calculator classes perform custom calculations. historically methods of these classes have been launched via backgroundworker, because lengthy, run-time wise.

recently converting application use new async support in c# , running same methods via task.run(), i'm noticing running 5 - 7% slower (which big deal in application).

is there obvious/expected reason why threadpool thread slower whatever backgroundworker doing?

i not making changes calculation logic; i'm merely passing calculation method task.run() (and awaiting it) before being supplied backgroundworker, i'm haven't introduced changes myself account speed reduction.

i expect see results if calculations fine-grained.

there overhead task.run: first, there's thread pool queues must navigated; second, there's marshaling of execution context (for security reasons); third, there's wrapper around operation catches exceptions.

if use single bgw own queue, queue simpler (and faster). marshaling of execution context happens once (for entire bgw), , there's no wrappers around each operation (there's 1 wrapper around bgw.dowork).

now, if using 1 bgw per calculation, then i'd expect task.run faster.

as alternative, check out task parallel library. calcuations, recommend parallel type (or plinq) on task.run unless need dynamic parallelism. if want more information, read parallel programming microsoft .net, particularly this image (but note "futures" better represented async/await , "pipelines" better represented tpl dataflow).


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -