C # Parallel class For loop compared with ordinary For loop time consumption performance,
1 static void Main (string [] args) 2 {3 var dt = DateTime. now; 4 var rand = new Random (DateTime. now. millisecond); 5 for (int I = 0; I <20; I ++) 6 {7 Thread. sleep (rand. next (100,100 0); 8} 9 var standerTime = (DateTime. now-dt ). totalMilliseconds; 10 Console. writeLine ("standard for loop, time consumed {0} millisecond", standerTime); 11 dt = DateTime. now; 12 Parallel. for (0, 20, I => 13 {14 Thread. sleep (rand. next (100,100 0); 15}); 16 var parallelTime = (DateTime. now-dt ). totalMilliseconds; 17 Console. writeLine ("parallel for loop, time consumed {0} millisecond", parallelTime); 18 Console. writeLine (); 19 if (standerTime <= parallelTime) 20 {21 Console. writeLine ("---------- standard for loop saves {0} millisecond ----------", (parallelTime-standerTime); 22} 23 else24 {25 Console. writeLine ("---------- saving {0} millisecond for parallel for loop ----------", (standerTime-parallelTime); 26} 27 Console. readKey (); 28}