How can I make this program run faster? On enter, I have a string from the console. For example:
1 1 7 3 2 0 0 4 5 5 6 2 1
On exit, it will be
6 20
Could you recommend a way to make this run faster?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
ArrayList<Integer> _arr = new ArrayList<Integer>();
while((line=br.readLine())!= null){
StringTokenizer tok = new StringTokenizer(line);
while (tok.hasMoreTokens()) {
_arr.add((Integer.parseInt(tok.nextToken())));
}
}
Algorithm(_arr);
}
private static void Algorithm(ArrayList<Integer> arr) {
int ascLength = 1,
descLength = 1,
ascSequenceStart = 0,
descSequenceStart = 0,
ascLongest = 1,
descLongest = 1,
summa = 0,
tmp1, tmp2;
for (int i = 1; i<arr.size(); i++) {
tmp1 = arr.get(i-1);
tmp2 = arr.get(i);
if(tmp2>tmp1){
ascLength++;
descLength = 1;
} else if (tmp2<tmp1) {
descLength++;
ascLength = 1;
} else if (tmp1==tmp2){
descLength++;
ascLength++;
}
if (ascLength > ascLongest) {
ascLongest = ascLength;
ascSequenceStart = i - ascLength + 1;
}
if (descLength > descLongest) {
descLongest = descLength;
descSequenceStart = i - descLength + 1;
}
}
if (ascLongest > descLongest) {
for (int i = ascSequenceStart; i < (ascSequenceStart+ascLongest); i++) {
summa += arr.get(i);
}
System.out.print(String.valueOf(ascLongest)+" "+String.valueOf(summa));
} else {
for (int i = descSequenceStart; i < (descSequenceStart+descLongest); i++) {
summa += arr.get(i);
}
System.out.print(String.valueOf(descLongest)+" "+String.valueOf(summa));
}
}
}