- //Prepare the query
- int[] modeThreeIsZero = (from num in source.AsParallel()
- where (num % 3) == 0
- orderby num descending
- select num).ToArray();
- namespace PLINQ_Demo {
- class Program {
- static DateTime startTime;
- static void Main(string[] args) {
- ProcessIntDataNormalMode();
- ProcessIntDataNormalMode();
- Console.ReadLine();
- }
- private static void ProcessIntDataNormalMode() {
- //record current time
- startTime = DateTime.Now;
- //Get a random large array of integers
- int[] source = Enumerable.Range(1, 10000000).ToArray();
- //Prepare the query
- int[] modeThreeIsZero = (from num in source.AsParallel() where(num % 3) == 0 orderby num descending select num).ToArray();
- //timer
- TimeSpan ts = DateTime.Now.Subtract(startTime);
- //Process and show the result
- Console.WriteLine("{0} numbers found as result. \n Time Elapsed: {1} Seconds:MilliSeconds in Normal mode", modeThreeIsZero.Count(), ts.Seconds + ":" + ts.Milliseconds);
- }
- private static void ProcessIntDataParallelMode() {
- //record current time
- startTime = DateTime.Now;
- //Get a random large array of integers
- int[] source = Enumerable.Range(1, 10000000).ToArray();
- //Prepare the query
- int[] modeThreeIsZero = (from num in source.AsParallel() where(num % 3) == 0 orderby num descending select num).ToArray();
- //timer
- TimeSpan ts = DateTime.Now.Subtract(startTime);
- //Process and show the result
- Console.WriteLine("{0} numbers found as result \n Time Elapsed: {1} [Seconds:MilliSeconds] in parallel mode.", modeThreeIsZero.Count(), ts.Seconds + ":" + ts.Milliseconds);
- }
- }
- }
Output


Join the conversation! Your thoughts help the community grow.