2017-04-26 127 views
0

我目前正在学习Java。下面是我写的一个简单的Java程序的方法列表。这些方法中有什么突出的东西会导致程序的执行速度变慢?使用只包含6个整数的数组需要4秒钟的时间:Java方法定义执行速度很慢

编辑:这是整个程序的要求。我在Textpad中写了它。我意识到这不是最有效的算法。它做它应该做的事情,但要花很长时间才能做到。

import java.util.*; 
public class Supermarket 
{ 
    public static void main(String [] args) 
    { 
     int[] custTimes = 
     { 
      1, 6, 7, 4, 4, 3, 5, 1, 2, 1, 3, 6, 4 
     }; 

     int checkOuts = 6; 
     int answer; 
     answer = Solution.solveSuperMarketQueue(custTimes, checkOuts); 
     System.out.println("Answer is " + answer); 
    } 
}//~public class Supermarket... 

class Solution 
{ 
    static int myTotal; 
    static int solveSuperMarketQueue(int[] customers, int n) 
    { 
     // ******************* INITIALIATION *********************** 
     myTotal = 0; 
     int len = customers.length; // length of customer queue 
     if (len < 1) 
     { 
      return 0; 
     } 
     int[] till = new int[n]; // array to store all tills and till queues 
     int tillMin; // Minimum time 
     int tillMax; // Maximum time 

     // Put the customers into an arraylist: 
     ArrayList<Integer> times = new ArrayList<Integer>(); 
     for (int i = 0; i < len; i = i + 1) 
     { 
      times.add(i, customers[i]); 
     } 

     // create the array of tills and set all queue intial values to 0 
     for (int i = 0; i < n; n = n + 1) 
     { 
      till[i] = 0; 
     } 
     // Move the queue to tills to start off 
     ReturnPair result = copyQueue(till, times); 
     till = result.getArr(); 
     times = result.getList(); 
     int s = times.size(); 

     tillMax = getMaxTime(till); 
     tillMin = getMinTime(till); 

     // ***************** END OF INITIALIATION ****************** 

     // *****************MAIN LOOP ****************************** 


     while (tillMax > 0) 
     { 
      // Find customer(s) with least time use that time to move all queues 
      // and update myTotal time. 
      // STEP 1: get minimum time in tills array (ignore zero) 
      tillMin = getMinTime(till); 
      // STEP 2: subtract minimum value from all the tills, but not if till has a zero 
      if (tillMin > 0) 
      { 
       till = subtractTime(till, tillMin); 
      } 
      // Move the queue to tills 
      if (s > 0) 
      { 
       result = copyQueue(till, times); 
       till = result.getArr(); 
       times = result.getList(); 
      } 

      tillMax = getMaxTime(till); 
      tillMin = getMinTime(till); 
     } 
     return myTotal; 

     // **************** END OF LOOP ***************************** 

    }//~public static int solveS... 


    // ****************** METHODS ********************************** 

    // Method to move queue foward 
    // For each till, a time is copied from the customer array. 
    // The values are copied in order. 
    // The value is coped only if array value is zero. 
    private static ReturnPair copyQueue(int[] arr, ArrayList<Integer> arrList) 
    { 
     int n = arr.length; // for each till... 
     for (int i = 0; i < n; i = i + 1) 
     { 
      if (arr[i] == 0 && arrList.size() > 0) // only copy if it current till value is 0 AND arrayList value exists 
      { 
       arr[i] = arrList.get(0); 
       arrList.remove(0); 
      } 
     } 

     // returns an instance of the object myResult which is a container for an array and an arraylist 
     return new ReturnPair(arr, arrList); 
    } 

    // Method to get minimum time from array (but not zero). 
    private static int getMinTime(int[] arr) 
    { 
     int minValue = 0; 

     // make sure arr[i] isn't zero. 
     for (int i = 0; i < arr.length; i = i + 1) 
     { 
      if (arr[i] != 0) 
      { 
       minValue = arr[i]; 
       break; 
      } 
     } 

     // Find minimum value that isn't zero. 
     for (int i = 1; i < arr.length; i = i + 1) 
     { 
      if (arr[i] != 0 && arr[i] < minValue) 
      { 
       minValue = arr[i]; 
      } 
     } 
     return minValue; 
    }//~static int getMinTime(in... 

    // Method to subtract minimum time from tills 
    private static int[] subtractTime(int[] arr, int min) 
    { 
     int n = arr.length; 
     for (int i = 0; i < n; i = i + 1) 
     { 
      if (arr[i] != 0) 
      { 
       arr[i] = arr[i] - min; 
      } 
     } 
     // update myTotal 
     myTotal = myTotal + min; 
     return arr; 
    }//~static void subtractTime... 

    private static int getMaxTime(int[] arr) 
    { 
     int maxValue = arr[0]; 
     for (int i = 1; i < arr.length; i = i + 1) 
     { 
      if (arr[i] > maxValue) 
      { 
       maxValue = arr[i]; 
      } 
     } 
     return maxValue; 
    } 
}//~class Solution... 

// Special class designed to return an array and an array list as an object 
class ReturnPair 
{ 
    // set up fields 
    int[] newArr; 
    ArrayList<Integer> newArrList; 

    // define method 
    public ReturnPair(int[] first, ArrayList<Integer> second) 
    { 
     this.newArr = first; 
     this.newArrList = second; 
    } 

    public int[] getArr() 
    { 
     return newArr; 
    } 

    public ArrayList<Integer> getList() 
    { 
     return newArrList; 
    } 
} 
+0

你是怎么测量4秒的?曾听说过微基准效应?一些阅读; http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java – 2017-04-26 16:14:54

+1

你能告诉我们一个我们可以重现的例子吗?例如您可以在ideone.com上运行的东西。 –

+0

总元素? –

回答

3
for (int i = 0; i < n; n = n + 1) 

这条线被递增n代替i。它将循环直到n溢出。它应该是:

for (int i = 0; i < n; i++) 

因为INT阵列反正初始化为0,可以彻底删除该循环。

+0

非常感谢您的帮助。这是我累了时所做的那种打字错误。很难发现什么时候没有错误。此外,我不知道数组被初始化为0,所以也要感谢。 – Jason210