Algs4-1.4.37自动装箱的性能代价

1.437自动装箱的性能代价。通过实验在你的计算机上计算使用自动装箱和自动拆箱所付出的性能代价。实现一个FixedCapacityStackOfInts,并使用类似DoublingRatio的用例比较它和泛型FixedCapacityStack<Integer>在进行大量push()和pop()操作时的性能。
答:
图片
public class FixedCapacityStackOfInts
{
    private int[] a;
    private int N;
    public FixedCapacityStackOfInts(int cap)
    {a=new int[cap];}
   
    public void push(int item)
    {a[N++]=item;}
   
    public int pop()
    {return a[--N];}
   
    public boolean isEmpty()
    {return N==0;}
   
    public int size()
    {return N;}
   
    public boolean isFull()
    {return N==a.length;}
}//end class
public class FixedCapacityStack<Item>
{
    private Item[] a;
    private int N;
    public FixedCapacityStack(int cap)
    {a=(Item[]) new Object[cap];}
   
    public void push(Item item)
    {
        if(N==a.length) resize(2*a.length);
        a[N++]=item;
    }
   
    public Item pop()
    {
        Item item= a[--N];
        a[N]=null;
        if(N>0 && N==a.length/4)  resize(a.length/2);
        return item;
    }
   
    public boolean isEmpty()
    {return N==0;}
   
    public int size()
    {return N;}
   
    private void resize(int max)
    {
        Item[] temp=(Item[]) new Object[max];
        for(int i=0;i<N;i++)
             temp[i]=a[i];
        a=temp;
    }
   
   
    public static void main(String[] args)
    {
        FixedCapacityStack<String> s;
        s=new FixedCapacityStack<String>(Integer.parseInt(args[0]));
        while(!StdIn.isEmpty())
        {
            String item=StdIn.readString();
            if(!item.equals("-"))
                s.push(item);
            else if (!s.isEmpty())
                StdOut.printf(s.pop()+" ");
        }//end while
        StdOut.println("("+s.size()+" left on stack)");
    }//end main
}//end class
public class E1d4d37A
{
    public static double timeTrial(long N)
    {
        FixedCapacityStackOfInts sInts=new FixedCapacityStackOfInts(1);
        Stopwatch timer=new Stopwatch();       
        for(long i=0;i<N;i++)
        {
           sInts.push(1);
           sInts.pop();
        }
        return timer.elapsedTime();
    }
   
    public static void main(String[] args)
    {
       double prev=timeTrial(50);
       for (long N=100;N<Long.MAX_VALUE/2;N+=N)
       {
           double time=timeTrial(N);
           StdOut.printf("%15d %12.4f  ",N,time);
           StdOut.printf("%5.1f ",time/prev);
           prev=time;
       }
    }
}
//
public class E1d4d37B
{
    public static double timeTrial(long N)
    {
        FixedCapacityStack sInteger=new FixedCapacityStack(1);
        Stopwatch timer=new Stopwatch();       
        for(long i=0;i<N;i++)
        {
           sInteger.push(1);
           sInteger.pop();
        }
        return timer.elapsedTime();
    }
   
    public static void main(String[] args)
    {
       double prev=timeTrial(50);
       for (long N=100;N<Long.MAX_VALUE/2;N+=N)
       {
           double time=timeTrial(N);
           StdOut.printf("%15d %12.4f  ",N,time);
           StdOut.printf("%5.1f ",time/prev);
           prev=time;
       }
    }

原文地址:https://www.cnblogs.com/longjin2018/p/9854534.html