Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace BoxingTest
- {
- public abstract class BoxedVal
- {
- public abstract BoxedVal Add(BoxedVal other);
- public abstract bool LessThan(BoxedVal other);
- public abstract bool GreaterThan(BoxedVal other);
- }
- public class BoxedInt : BoxedVal
- {
- public int Value;
- public BoxedInt(int value)
- {
- Value = value;
- }
- public override BoxedVal Add(BoxedVal other)
- {
- BoxedInt i = other as BoxedInt;
- return new BoxedInt(Value + i.Value);
- }
- public override bool LessThan(BoxedVal other)
- {
- BoxedInt i = other as BoxedInt;
- return Value < i.Value;
- }
- public override bool GreaterThan(BoxedVal other)
- {
- BoxedInt i = other as BoxedInt;
- return Value > i.Value;
- }
- }
- class Program
- {
- private static int Fib(int nn)
- {
- BoxedVal one = new BoxedInt(1);
- BoxedVal a = one;
- BoxedVal b = one;
- BoxedVal n = new BoxedInt(nn);
- BoxedVal thousand = new BoxedInt(1000);
- for (BoxedVal i = new BoxedInt(2); i.LessThan(n); i = i.Add(one))
- {
- BoxedVal c = a.Add(b);
- a = b;
- b = c;
- if (b.GreaterThan(thousand))
- {
- a = one;
- b = one;
- }
- }
- return (b as BoxedInt).Value;
- }
- static void Main(string[] args)
- {
- int times = 5;
- int n = 20000000;
- int total = 0;
- for (int i = 0; i < times; i++)
- {
- var start = DateTime.Now;
- int val = Fib(n);
- int dt = (DateTime.Now - start).Milliseconds;
- total += dt;
- Console.WriteLine(val);
- Console.WriteLine("Elapsed: {0} ms", dt);
- }
- Console.WriteLine("Average: {0} ms", total / times);
- Console.ReadLine();
- }
- }
- }
- abstract class BoxedVal
- {
- public abstract BoxedVal Add(BoxedVal other);
- public abstract boolean LessThan(BoxedVal other);
- public abstract boolean GreaterThan(BoxedVal other);
- }
- class BoxedInt extends BoxedVal
- {
- public int Value;
- public BoxedInt(int value)
- {
- Value = value;
- }
- @Override
- public BoxedVal Add(BoxedVal other)
- {
- BoxedInt i = (BoxedInt)other;
- return new BoxedInt(Value + i.Value);
- }
- @Override
- public boolean LessThan(BoxedVal other)
- {
- BoxedInt i = (BoxedInt)other;
- return Value < i.Value;
- }
- @Override
- public boolean GreaterThan(BoxedVal other)
- {
- BoxedInt i = (BoxedInt)other;
- return Value > i.Value;
- }
- }
- public class BoxingTest {
- private static int Fib(int nn)
- {
- BoxedVal one = new BoxedInt(1);
- BoxedVal a = one;
- BoxedVal b = one;
- BoxedVal n = new BoxedInt(nn);
- BoxedVal thousand = new BoxedInt(1000);
- for (BoxedVal i = new BoxedInt(2); i.LessThan(n); i = i.Add(one))
- {
- BoxedVal c = a.Add(b);
- a = b;
- b = c;
- if (b.GreaterThan(thousand))
- {
- a = one;
- b = one;
- }
- }
- return ((BoxedInt)b).Value;
- }
- public static void main(String[] args) {
- int times = 5;
- int n = 20000000;
- long total = 0;
- for (int i = 0; i<times;i++){
- long start = System.currentTimeMillis();
- int val = Fib(n);
- long dt = System.currentTimeMillis() - start;
- total+= dt;
- System.out.println(val);
- System.out.println("Elapsed: "+dt+" ms");
- }
- System.out.println("Average: "+(total/times)+" ms");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement