Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace BoxingTest
  8. {
  9. public abstract class BoxedVal
  10. {
  11. public abstract BoxedVal Add(BoxedVal other);
  12. public abstract bool LessThan(BoxedVal other);
  13. public abstract bool GreaterThan(BoxedVal other);
  14. }
  15.  
  16. public class BoxedInt : BoxedVal
  17. {
  18. public int Value;
  19.  
  20. public BoxedInt(int value)
  21. {
  22. Value = value;
  23. }
  24.  
  25. public override BoxedVal Add(BoxedVal other)
  26. {
  27. BoxedInt i = other as BoxedInt;
  28. return new BoxedInt(Value + i.Value);
  29. }
  30.  
  31. public override bool LessThan(BoxedVal other)
  32. {
  33. BoxedInt i = other as BoxedInt;
  34. return Value < i.Value;
  35. }
  36.  
  37. public override bool GreaterThan(BoxedVal other)
  38. {
  39. BoxedInt i = other as BoxedInt;
  40. return Value > i.Value;
  41. }
  42. }
  43.  
  44. class Program
  45. {
  46. private static int Fib(int nn)
  47. {
  48. BoxedVal one = new BoxedInt(1);
  49.  
  50. BoxedVal a = one;
  51. BoxedVal b = one;
  52.  
  53. BoxedVal n = new BoxedInt(nn);
  54.  
  55. BoxedVal thousand = new BoxedInt(1000);
  56.  
  57. for (BoxedVal i = new BoxedInt(2); i.LessThan(n); i = i.Add(one))
  58. {
  59. BoxedVal c = a.Add(b);
  60. a = b;
  61. b = c;
  62.  
  63. if (b.GreaterThan(thousand))
  64. {
  65. a = one;
  66. b = one;
  67. }
  68. }
  69. return (b as BoxedInt).Value;
  70. }
  71.  
  72. static void Main(string[] args)
  73. {
  74. int times = 5;
  75. int n = 20000000;
  76.  
  77. int total = 0;
  78.  
  79. for (int i = 0; i < times; i++)
  80. {
  81. var start = DateTime.Now;
  82. int val = Fib(n);
  83.  
  84. int dt = (DateTime.Now - start).Milliseconds;
  85. total += dt;
  86. Console.WriteLine(val);
  87. Console.WriteLine("Elapsed: {0} ms", dt);
  88. }
  89.  
  90. Console.WriteLine("Average: {0} ms", total / times);
  91.  
  92. Console.ReadLine();
  93.  
  94. }
  95. }
  96. }
  97.  
  98. abstract class BoxedVal
  99. {
  100. public abstract BoxedVal Add(BoxedVal other);
  101. public abstract boolean LessThan(BoxedVal other);
  102. public abstract boolean GreaterThan(BoxedVal other);
  103. }
  104.  
  105.  
  106. class BoxedInt extends BoxedVal
  107. {
  108. public int Value;
  109.  
  110.  
  111. public BoxedInt(int value)
  112. {
  113. Value = value;
  114. }
  115.  
  116. @Override
  117. public BoxedVal Add(BoxedVal other)
  118. {
  119. BoxedInt i = (BoxedInt)other;
  120. return new BoxedInt(Value + i.Value);
  121. }
  122.  
  123. @Override
  124. public boolean LessThan(BoxedVal other)
  125. {
  126. BoxedInt i = (BoxedInt)other;
  127. return Value < i.Value;
  128. }
  129.  
  130. @Override
  131. public boolean GreaterThan(BoxedVal other)
  132. {
  133. BoxedInt i = (BoxedInt)other;
  134. return Value > i.Value;
  135. }
  136. }
  137.  
  138. public class BoxingTest {
  139.  
  140. private static int Fib(int nn)
  141. {
  142. BoxedVal one = new BoxedInt(1);
  143.  
  144. BoxedVal a = one;
  145. BoxedVal b = one;
  146.  
  147. BoxedVal n = new BoxedInt(nn);
  148.  
  149. BoxedVal thousand = new BoxedInt(1000);
  150.  
  151. for (BoxedVal i = new BoxedInt(2); i.LessThan(n); i = i.Add(one))
  152. {
  153. BoxedVal c = a.Add(b);
  154. a = b;
  155. b = c;
  156.  
  157. if (b.GreaterThan(thousand))
  158. {
  159. a = one;
  160. b = one;
  161. }
  162. }
  163. return ((BoxedInt)b).Value;
  164. }
  165.  
  166. public static void main(String[] args) {
  167.  
  168. int times = 5;
  169. int n = 20000000;
  170.  
  171. long total = 0;
  172.  
  173.  
  174. for (int i = 0; i<times;i++){
  175. long start = System.currentTimeMillis();
  176.  
  177. int val = Fib(n);
  178. long dt = System.currentTimeMillis() - start;
  179. total+= dt;
  180. System.out.println(val);
  181. System.out.println("Elapsed: "+dt+" ms");
  182. }
  183.  
  184. System.out.println("Average: "+(total/times)+" ms");
  185.  
  186. }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement