using System.Linq;
using System.Web.Mvc;
using MyApplication.Models;
namespace MyApplication.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
string loremIpsum = LoremIpsum.Copy();
var query = from word in loremIpsum.Trim().Split(' ').AsParallel()
select word.ToUpper();
string alteration = string.Join(" ", query);
return View();
}
}
}
PLINQ, LINQ with processing parallelized on multiple threads, is yet another thing I have read about in C# 4.0 in a Nutshell by Joseph and Ben Albahari. As you might guess, LoremIpsum.Copy(); above returns the copy of lorem ipsum in a string. I set a breakpoint at return View(); above in Visual Studio to see how .AsParallel() would bias the outcome. As it turned out, the words came back in an odd order, as to be expected, although this did not happen after I reran the code a few times. (Adding .AsOrdered() after .AsParallel() forces the ordering to be as though it did not happen on separate threads.)
Copy out of page 874 of the book:
- In recent times, CPU clock speeds have stagnated and manufactures have shifted their focus to increasing core counts. This is problematic for us as programmers because our standard single-threaded code will not automatically run faster as a result of those extra cores.
- Programming to leverage multicore or multiple processors is called parallel programming. This is a subset of the broader concept of multithreading.