gpt4 book ai didi

Java 方法定义显着降低了执行速度

转载 作者:行者123 更新时间:2023-12-01 20:27:53 25 4
gpt4 key购买 nike

我目前正在学习Java。下面是我编写的一个简单 Java 程序中的方法列表。这些方法中是否有什么突出的地方会导致程序执行速度非常慢?使用仅包含 6 个整数的数组执行需要四秒钟:

已编辑:这是所要求的整个程序。我用文本板写的。我意识到这不是最有效的算法。它做了它应该做的事情,但需要很长时间才能完成。

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;
}
}

最佳答案

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

这一行递增的是n,而不是i。它将循环直到n溢出。应该是:

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

因为 int 数组无论如何都会初始化为 0,所以您可以完全删除此循环。

关于Java 方法定义显着降低了执行速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43639509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com