fork(1) download
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7.  
  8.  
  9. namespace ConsoleApp
  10. {
  11.  
  12. public class Program
  13. {
  14. static int FibRec(int p1, int p2, int n)
  15. {
  16. return n == 0 ? p1 : FibRec(p2, p1 + p2, n - 1);
  17. }
  18. static int Fib(int n) { return FibRec(0, 1, n); }
  19.  
  20. static int Fibonachi(int n, int p1 = 0, int p2 = 1)
  21. {
  22. if (n <= 1) return p1;
  23. int p;
  24. for (int j = 2; j < n; j++)
  25. {
  26. p = p1;
  27. p1 = p2;
  28. p2 = p2 + p;
  29. }
  30. return p2;
  31. }
  32.  
  33. private static void Main(string[] args)
  34. {
  35. var sp = new Stopwatch();
  36. sp.Start();
  37. int x = 0;
  38. for (int i = 0; i < 100000; i++) x += Fib(1000);
  39. sp.Stop();
  40. Console.WriteLine("recurse : {0}", sp.Elapsed);
  41. sp = new Stopwatch();
  42. sp.Start();
  43. x = 0;
  44. for (int i = 0; i < 100000; i++) x += Fibonachi(1000);
  45. sp.Stop();
  46. Console.WriteLine("loop : {0}", sp.Elapsed);
  47. Console.ReadKey();
  48. }
  49.  
  50. }
  51. }
  52.  
Success #stdin #stdout 0.74s 33920KB
stdin
Standard input is empty
stdout
recurse : 00:00:00.4766598
loop : 00:00:00.2274692