⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.java

📁 PracticalJAVACode 的源码
💻 JAVA
字号:
class Test
{
  public final static int ArraySize = 10;
  public int[] addArrays1(int[] arr1, int[] arr2)
  {
    int[] result = new int[ArraySize];
    IllegalArgumentException exc = new IllegalArgumentException
                                   ("arrays are invalid");

    if (arr1 != null && arr2 != null &&
        arr1.length == ArraySize && arr2.length == ArraySize)
    {
      for (int i=0;i<ArraySize; i++)
        result[i]=arr1[i]+arr2[i];
    }
    else
      throw exc;
    return result;
  }
  public int[] addArrays2(int[] arr1, int[] arr2)
  {
    if (arr1 != null && arr2 != null &&
        arr1.length == ArraySize && arr2.length == ArraySize)
    {
      int[] result = new int[ArraySize];
      for (int i=0;i<ArraySize; i++)
        result[i]=arr1[i]+arr2[i];
      return result;
    }
    throw new IllegalArgumentException("arrays are invalid");
  }
  public static void main(String args[])
  {
    int val=Integer.parseInt(args[0]);
    Test t = new Test();
    int[] a1 = new int[ArraySize];
    int[] a2 = new int[ArraySize];
    for (int j=0; j<ArraySize; j++)
      a1[j]=a2[j]=j;

    long start = System.currentTimeMillis();
    for (int i=0; i<val; i++)
    {
      try {
        t.addArrays1(a1,a2);
      }
      catch(IllegalArgumentException e){}
    }
    long end = System.currentTimeMillis();
    System.out.println("add inefficient " + (end-start));
    long num = end-start;

    start = System.currentTimeMillis();
    for (int i=0; i<val; i++)
    {
      try {
        t.addArrays2(a1,a2);
      }
      catch(IllegalArgumentException e){}
    }
    end = System.currentTimeMillis();
    System.out.println("add efficient " + (end-start));
    num = num/(end-start);
    System.out.println("effecient is " + num + " times faster");
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -