c# - Strangely slow: what happened when TakeWhile met AsParallel? -



c# - Strangely slow: what happened when TakeWhile met AsParallel? -

i know wrong usage of linq may cause lower performance time it's strange.

when phone call "asparallel.takewhile.asparallel.forall", it's much slower "asparallel.takewhile.forall". can explain why?

using system; using system.diagnostics; using system.linq; namespace consoleapplication1 { class programme { static void main(string[] args) { action<int> donothing = => { }; var stopwatch = stopwatch.startnew(); enumerable.range(1, 100).asparallel().takewhile(m => m < 10) .forall(donothing); var ticks1 = stopwatch.elapsedticks; stopwatch.restart(); enumerable.range(1, 100).asparallel().takewhile(m => m < 10) .asparallel() // spend much more time asparallel .forall(donothing); var ticks2 = stopwatch.elapsedticks; console.writeline("ticks without asparallel: {0}\r\n asparallel: {1}", ticks1, ticks2); //ticks without asparallel: 87956 //with asparallel: 6688708 console.read(); } } }

when phone call asparallel, framework starts split work smaller parts can scheduled on different cores. these parts later needed merge result. causes overhead.

you have made query parallel first "asparallel" , when phone call sec time partition happens again, causing more overhead giving no performance boost.

see understanding speedup in plinq more information.

c# linq plinq

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -