fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6.  
  7. public class Program {
  8. public static void Main(string[] args) {
  9. var sw = new Stopwatch();
  10.  
  11. var array = new double[10000000];
  12. for (int i = 0; i < array.Length; i++) {
  13. array[i] = Math.Exp(12);
  14. }
  15.  
  16. double tmp;
  17.  
  18. {
  19. sw.Reset();
  20. sw.Start();
  21.  
  22. for (int i = 0; i < array.Length; i++) {
  23. tmp = Math.Tanh(Math.Log(array[i]) / 7);
  24. }
  25.  
  26. sw.Stop();
  27. Console.WriteLine("Loop using for: {0}ms", sw.Elapsed.TotalMilliseconds);
  28. }
  29.  
  30. {
  31. sw.Reset();
  32. sw.Start();
  33.  
  34. foreach(double d in array){
  35. tmp = Math.Tanh(Math.Log(d) / 7);
  36. }
  37.  
  38. sw.Stop();
  39. Console.WriteLine("Loop using foreach: {0}ms", sw.Elapsed.TotalMilliseconds);
  40. }
  41.  
  42. }
  43. }
  44.  
  45.  
Success #stdin #stdout 4.59s 115264KB
stdin
Standard input is empty
stdout
Loop using for: 1936.1308ms
Loop using foreach: 1931.209ms