Take a look of this LINQ code, which use only 1 core on my quad core processor desktop pc.
IEnumerable arr = Enumerable.Range(1, 4000000);
var x =
from y in arr
where IsPrime(y)
select y.ToString();
List mylist = x.ToList();
Console.WriteLine(mylist.Count.ToString());
To take advantage of the other processors, we just have to change one line.
IEnumerable arr = Enumerable.Range(1, 4000000);
var x =
from y in arr.AsParallel()
where IsPrime(y)
select y.ToString();
List mylist = x.ToList();
Console.WriteLine(mylist.Count.ToString());
Isn't cool?
For More Coding Tips & Tricks, subscribe now.
No comments:
Post a Comment