Taking Advantage of Parallelism in .NET

Microservices, Data stream Processing, Event-Driven Architecture, CQRS… a lot of terms and buzzwords that represents the capacity of run process asynchronously in our softwares.

And they are good, but lets have a little break to think what kind of power we have right in our hands: the modern CPUs.

They usually have more than one physical core, and still manage to emulate more.

Recently, I read that Microsoft will launch a new version of its operation system: Windows 10 Workstations edition for power users. It will support 4 physical CPUs.

So, lets take advantage of it.

Usually we use to think algorithms in sequential steps, and that is right, because it depends of the result of each interaction to go to next step. But apps usually have a lot of algorithms running, and in some cases they can be independent of the others. Also we have typical cases where it is possible have an independent thread running in the system. For example I/O Bound.

Have you thought if is really necessary freeze the whole system waiting a conclusion of a HTTP communication? What about write a lot of data in disk? If you have to post the same data to five different HTTP endpoints, can it done parallel?

But for now, lets learn how to use it with C#.

Lets assume a classical mathematical problem, factorial computation:

static int GetFactorial(int n)
    return (n >= 1) ? (n * GetFactorial(n - 1)) : 1;

So, lets calculate factorial of 900, 100000 times, in a simple way:

static void Main(string[] args)
    //pause to stabilize the system
    var stopwatch = new Stopwatch();

    var number = 900;
    var timesToCalculate = 100000;

    // calculating N times the result of nuber factorial
    for (int i = 0; i < timesToCalculate; i++)

    Console.WriteLine($"{number}! calculated {timesToCalculate} times took: {stopwatch.ElapsedMilliseconds}");

And here we have the first result:

Let me introduce you the Task, a simple way to create threads in .NET.

There are a kind of “computer science philosophy” behind the difference and definitions, but a smart way to think can be: Tasks are easer to handle and work fine for computations with a result, like mathematical operations. Threads have a high cost to operation system, and a good use case can be something that you will constantly monitoring/handling.

Lets apply it to the code:

var tasks = new Task[timesToCalculate];

for (int i = 0; i < timesToCalculate; i++)
    tasks[i] = Task.Factory.StartNew(() => GetFactorial(number));


From the original 2202 milliseconds we were able to improve to 1687… seems we have some overhead costs, but even do it was an improvement.

Now I want to show you the “Parallel For”, a resource introduced in the .NET 4. When we have to do simple operations the results can be questioned (because some operations are so simple that does not pay the cost of divide the task among the cores).

Just to have an ideia:

static void Main(string[] args)
    var total = 1000000;
    var stopwatch = new Stopwatch();

    //pause to stabilize the system

    // normal for
    for (int i = 0; i < total; i++) ;
    Console.WriteLine($"'normal for' elapsed milliseconds: {stopwatch.ElapsedMilliseconds}");

    //pause to stabilize the system


    // parallel for
    Parallel.For(0, total, i => { });
    Console.WriteLine($"'parallel for' elapsed milliseconds: {stopwatch.ElapsedMilliseconds}");


This code jus run empty for commands, and here are the results:


One painter can took almost twice times to paint 300m² than two painter. But what about 1m²? Probably the second one will disturb more than help!

And in some cases there is no possible improve the process, for instance, 9 pregnant women can’t do a baby in one month.

But lets apply it to our Factorial scenario:

Parallel.For(0, timesToCalculate, (i) => {

Now, lets see the new results:

How cool was that!? We manage to drop the time from 2202 milliseconds to 909 🙂

You probable are asking yourself what will happen if mix the parallel for with the Tasks? To be honesty, that does not make sense, because it will be thread over thread. Threads has overheads costs, thinks like sync results, monitoring, memory managements… so, use it wisely 😉

But it is really easy to apply parallelism using .NET. The framework can handle and calculate how to use the hardware properly. And if you have just one core, there is no error or problem.

And you can control it easy as well, for instance, using “parallel for” is possible set the max amount of core it will be used.

Also, you can spend some time taking a look at PLINQ.

Enjoy it.



With more than 15 years of experience developing softwares and technologies, talking about startups, trends and innovation, today my work is focused to be CTO, Software Architect, Technical Speaker, Technical Consultant and Entrepreneur.

From Brazil, currently lives in Portugal working at https://www.farfetch.com as Software Architect, besides to keep projects in Brazil, like http://www.almocando.com.br/


818 thoughts on “Taking Advantage of Parallelism in .NET”

  1. Pingback: Google
  2. Pingback: dual vibrator
  3. Pingback: amazon offers
  4. Pingback: دليلي
  5. Pingback: termeh
  6. Pingback: Belize inn
  7. Pingback: 脱毛
  8. Pingback: nebbia leginy
  9. Pingback: گوگل adwords
  10. Pingback: PHP login script
  11. Pingback: کولر مسجد
  12. Pingback: persian tar
  13. Pingback: free resume
  14. Pingback: سیستم cctv
  15. Pingback: خرید vpn
  16. Pingback: خرید vpn
  17. Pingback: سیستم cctv
  18. Pingback: خرید vpn
  19. Pingback: love tips
  20. Pingback: خرید vpn
  21. Pingback: خرید طلا
  22. Pingback: سیستم cctv
  23. Pingback: فروش طلا
  24. Pingback: IPhone bulk
  25. Pingback: سیستم cctv
  26. Pingback: خرید طلا
  27. Pingback: خرید vpn
  28. Pingback: gratis inserate
  29. Pingback: خرید vpn
  30. Pingback: p spot stimulation
  31. Pingback: سیستم cctv
  32. Pingback: خرید vpn
  33. Pingback: hp printer drivers
  34. Pingback: rotating dildo
  35. Pingback: sex toys orgasm
  36. Pingback: clitoral vibrators
  37. Pingback: sheet
  38. Pingback: 50 shades vibrator
  39. Pingback: beauty
  40. Pingback: credit repair
  41. Pingback: خرید طلا
  42. Pingback: pets mega malls
  43. Pingback: rampant rabbit
  44. Pingback: nipple play
  45. Pingback: خرید طلا
  46. Pingback: ساب زنی
  47. Pingback: massive dildo
  48. Pingback: dani daniels
  49. Pingback: سیستم cctv
  50. Pingback: خرید vpn
  51. Pingback: خرید طلا
  52. Pingback: dildo
  53. Pingback: best penis pump
  54. Pingback: سیستم cctv
  55. Pingback: olcsó tanfolyam
  56. Pingback: look at this now
  57. Pingback: Lastminute
  58. Pingback: glass toy
  59. Pingback: sex toy haul
  60. Pingback: mom sexually
  61. Pingback: خرید طلا
  62. Pingback: سیستم cctv
  63. Pingback: masturbator lube
  64. Pingback: hydroquinone
  65. Pingback: lubricant review
  66. Pingback: hp drivers
  67. Pingback: iphone repair
  68. Pingback: orgasm
  69. Pingback: mini massager
  70. Pingback: خرید vpn
  71. Pingback: organic besan
  72. Pingback: realistic vibe
  73. Pingback: bahis siteleri
  74. Pingback: kona coffee beans
  75. Pingback: miniclip
  76. Pingback: bahis siteleri
  77. Pingback: flying shark
  78. Pingback: bahis siteleri
  79. Pingback: bet online
  80. Pingback: bahis siteleri
  81. Pingback: bahis siteleri
  82. Pingback: bahis siteleri
  83. Pingback: goldenbah's
  84. Pingback: penis size
  85. Pingback: bondage -bitcoin
  86. Pingback: clit vibator
  87. Pingback: Morgellons Cure
  88. Pingback: bunny vibrator
  89. Pingback: clash royale hack
  90. Pingback: imoti
  91. Pingback: shooting jackets
  92. Pingback: PHP Scripts
  93. Pingback: Sandskädda
  94. Pingback: pegging sex toys
  95. Pingback: solaray
  96. Pingback: life like dildo
  97. Pingback: moms bundle
  98. Pingback: Proper Order
  99. Pingback: join illuminati
  100. Pingback: sex funiya
  101. Pingback: matka result
  102. Pingback: jageze
  103. Pingback: free dating site
  104. Pingback: Get free backlinks
  105. Pingback: payday
  106. Pingback: cursos gratis
  107. Pingback: Equipment
  108. Pingback: garudaqq android
  109. Pingback: p3d coin
  110. Pingback: hydration
  111. Pingback: Phone Repairs
  112. Pingback: Instagram unfollow
  113. Pingback: psychic
  114. Pingback: psychic
  115. Pingback: Computer City
  116. Pingback: Vancouver Escorts
  117. Pingback: Africans online
  118. Pingback: gps tracker
  119. Pingback: drivers
  120. Pingback: sex toy experience
  121. Pingback: respuestas ceneval
  122. Pingback: golf lessons
  123. Pingback: prayer
  124. Pingback: brazilian hair
  125. Pingback: sex life
  126. Pingback: remy hair
  127. Pingback: Indian hair
  128. Pingback: malaysian hair
  129. Pingback: better health ways
  130. Pingback: peruvian hair
  131. Pingback: sexier heat up
  132. Pingback: Vibrator Bullet
  133. Pingback: Shower
  134. Pingback: fantasy novel
  135. Pingback: first dildo
  136. Pingback: travel news
  137. Pingback: nasal
  138. Pingback: prazdniki zavtra
  139. Pingback: Cheap project app
  140. Pingback: pubg tshirt
  141. Pingback: dialing codes
  142. Pingback: tracing video
  143. Pingback: christ movie
  144. Pingback: Atorvastatin
  145. Pingback: Hydroquinone
  146. Pingback: Hydroquinone
  147. Pingback: sportotobet
  148. Pingback: Cryptocurrency
  149. Pingback: FRP floor cover
  150. Pingback: mermaid series
  151. Pingback: satisfyer pro
  152. Pingback: hosting
  153. Pingback: Americab.net
  154. Pingback: Online cialis
  155. Pingback: Viagra uk
  156. Pingback: Cialis prices
  157. Pingback: Generic viagra
  158. Pingback: Online cialis
  159. Pingback: ROUND
  160. Pingback: Sports news
  161. Pingback: trah_net_621
  162. Pingback: sasha grey toy
  163. Pingback: make money online
  164. Pingback: beginner anal toys
  165. Pingback: giant dildo
  166. Pingback: What Is Lol
  167. Pingback: toy haul
  168. Pingback: anal beads
  169. Pingback: cock pump
  170. Pingback: AdamandEve
  171. Pingback: wand massager
  172. Pingback: Subscription Box
  173. Pingback: prostate toy
  174. Pingback: Film izle
  175. Pingback: vibrator review
  176. Pingback: kinky sex toy
  177. Pingback: sarah rae vargas
  178. Pingback: sex toy haul
  179. Pingback: pocket masturbator
  180. Pingback: strap on sex toy
  181. Pingback: pictures
  182. Pingback: 13 sentyabrya 9060
  183. Pingback: DraftKings DFS
  184. Pingback: Tire Pressure
  185. Pingback: friv
  186. Pingback: Gifts and gadgets
  187. Pingback: Brisbane painters
  188. Pingback: e stim machine
  189. Pingback: NFL DFS
  190. Pingback: College Basketball
  191. Pingback: College Football
  192. Pingback: g vibrator
  193. Pingback: Milf porn
  194. Pingback: quizhow
  195. Pingback: سكس
  196. Pingback: home inspections
  197. Pingback: Sexy cam
  198. Pingback: anal training toys
  199. Pingback: aflamsex
  200. Pingback: frases.top
  201. Pingback: Curency conveter
  202. Pingback: rottweiler puppy
  203. Pingback: Live sexy cams
  204. Pingback: Funny videos
  205. Pingback: Sexlive
  206. Pingback: Jumpstar car
  207. Pingback: Livesexgirls
  208. Pingback: Freexxx
  209. Pingback: Porn video
  210. Pingback: Sexy shemale
  211. Pingback: Hot babe
  212. Pingback: Iphone deal
  213. Pingback: Cheap iphone cases
  214. Pingback: Cases for iphone X
  215. Pingback: سكس
  216. Pingback: سكس مترجم
  217. Pingback: before having sex
  218. Pingback: marketing apps
  219. Pingback: Dog Kennels
  220. Pingback: best vibrater
  221. Pingback: sex toy
  222. Pingback: butt plugs
  223. Pingback: Cialis online
  224. Pingback: سكس
  225. Pingback: Dog Run NZ
  226. Pingback: MILF
  227. Pingback: سكس مترجم
  228. Pingback: thai lotto world
  229. Pingback: 2018 NFL Schedule
  230. Pingback: see thru panties
  231. Pingback: Dr Garo Kassabian
  232. Pingback: gaming
  233. Pingback: nyc brownstone
  234. Pingback: USED CARS
  235. Pingback: criminal lawyer
  236. Pingback: summer camp china
  237. Pingback: russian holidays
  238. Pingback: whatsapp
  239. Pingback: lesbian vibrator
  240. Pingback: lesbian strap ons
  241. Pingback: mp3 songs
  242. Pingback: aflampornhd
  243. Pingback: Musicpleer
  244. Pingback: female vibrators
  245. Pingback: Perfect Bedroom
  246. Pingback: bullet vibrator
  247. Pingback: luxury sex toys
  248. Pingback: sex lubricant
  249. Pingback: sex toys review
  250. Pingback: djswoope
  251. Pingback: Cellphone repair
  252. Pingback: Anal Lube Review
  253. Pingback: poker88
  254. Pingback: G Spot Vibrator
  255. Pingback: real feel penis
  256. Pingback: how to use a vibe
  257. Pingback: vibrator tutorial
  258. Pingback: MILF Porn
  259. Pingback: mp3juices
  260. Pingback: tubidy mp3
  261. Pingback: best pocket pussy
  262. Pingback: triple stimulator
  263. Pingback: Cialis 20mg
  264. Pingback: writeaessay
  265. Pingback: Buy cialis
  266. Pingback: ebooks for resale
  267. Pingback: Buy cialis
  268. Pingback: sobytiya
  269. Pingback: thai netflix
  270. Pingback: iPhone 6 Cable
  271. Pingback: iPhone Cable
  272. Pingback: Tecnologia
  273. Pingback: laptop app
  274. Pingback: رطوبت ساز
  275. Pingback: prazdnik noyabr
  276. Pingback: yoni masseur paris
  277. Pingback: bitcoin shopping
  278. Pingback: nintendo dope
  279. Pingback: Bike Stand NZ
  280. Pingback: Cellphone sale
  281. Pingback: سئو سایت
  282. Pingback: Security Firm
  283. Pingback: male masturbator
  284. Pingback: auto taxi
  285. Pingback: vibrator unboxing
  286. Pingback: datumoufukuiken
  287. Pingback: female vibrator
  288. Pingback: Rechnungswesen
  289. Pingback: Dog Playpen
  290. Pingback: فال هفتگی
  291. Pingback: Baby Playpen
  292. Pingback: interracial porn
  293. Pingback: Knights Templar
  294. Pingback: entrepreneur
  295. Pingback: strapon bunny
  296. Pingback: Best Wand Massager
  297. Pingback: Penis Pump
  298. Pingback: Best Anal Toy
  299. Pingback: Best Anal Lube
  300. Pingback: slovar efremovoy
  301. Pingback: iPad wholesale
  302. Pingback: Kegel Balls
  303. Pingback: Dominatrix Toys
  304. Pingback: Big Dildos
  305. Pingback: افلام سكس
  306. Pingback: mp3
  307. Pingback: g spot stimulator
  308. Pingback: penis extension
  309. Pingback: big dildo review
  310. Pingback: sex xlxx
  311. Pingback: Bestecktaschen
  312. Pingback: best vibrators
  313. Pingback: $1 hosting
  314. Pingback: apps for pc
  315. Pingback: mansion88
  316. Pingback: PhillyBite
  317. Pingback: Download New Music
  318. Pingback: Buy kona coffee
  319. Pingback: Kona coffee brands
  320. Pingback: bail bond
  321. Pingback: سكس
  322. Pingback: Buy kona coffee
  323. Pingback: junk car buyer
  324. Pingback: buy lion coffee
  325. Pingback: escape room athens
  326. Pingback: lion coffee blends
  327. Pingback: cat
  328. Pingback: Abraaj
  329. Pingback: purple hoverboard
  330. Pingback: Instagram
  331. Pingback: kona coffee brands
  332. Pingback: hosting
  333. Pingback: hummer hoverboard
  334. Pingback: Cbd cancer
  335. Pingback: lesbian clitoris
  336. Pingback: rough sex
  337. Pingback: buy iptv panel
  338. Pingback: seo service
  339. Pingback: Vanessa Lunnon
  340. Pingback: free phone cases
  341. Pingback: cell phone
  342. Pingback: terminal connector
  343. Pingback: it paslaugos
  344. Pingback: etf vs mutual fund
  345. Pingback: Roof Racks
  346. Pingback: طلای سفید
  347. Pingback: camping gifts
  348. Pingback: denver limo
  349. Pingback: Fast Start Amazon
  350. Pingback: Cheap online shop
  351. Pingback: FS 19 mods
  352. Pingback: apprenticeship
  353. Pingback: yoyo media
  354. Pingback: Endless game
  355. Pingback: digital music news
  356. Pingback: hotel deals
  357. Pingback: mp3
  358. Pingback: mp3
  359. Pingback: jesus quiz
  360. Pingback: Safe365
  361. Pingback: rottweiler puppy
  362. Pingback: Woonkamer ideeën
  363. Pingback: Dance Lessons
  364. Pingback: virtual world
  365. Pingback: happy games
  366. Pingback: celine
  367. Pingback: custom patches
  368. Pingback: Chicken Feeders
  369. Pingback: Adderall For sale
  370. Pingback: pillows
  371. Pingback: top vr games
  372. Pingback: beauty courses
  373. Pingback: nail art courses
  374. Pingback: click
  375. Pingback: prazdniki 9 marta
  376. Pingback: full apps download
  377. Pingback: smettere di fumare
  378. Pingback: سكس
  379. Pingback: game free download
  380. Pingback: Rockstarlyrics
  381. Pingback: Raw food Cafe Bali
  382. Pingback: Bun B illuminati
  383. Pingback: aquarium
  384. Pingback: Valentino Garavani
  385. Pingback: Prada
  386. Pingback: Yves Saint Laurent
  387. Pingback: Stella Mccartney
  388. Pingback: Breast Cancer
  389. Pingback: xxxmaduras
  390. Pingback: brindes
  391. Pingback: AtoZ Cancer Types
  392. Pingback: vardenafil online
  393. Pingback: buy levitra 10 mg
  394. Pingback: Interracial Porn
  395. Pingback: مه پاش
  396. Pingback: Cat Art
  397. Pingback: ixxx
  398. Pingback: beauty
  399. Pingback: coupon levitra

Leave a Reply