[英]Can i make following code any Faster
I would like to make the following code faster, without changing the reading/writing from standard console. 我想使以下代码更快,而无需更改从标准控制台进行的读取/写入。 The first line contains the number of inputs and the subsequent lines contain a set of
Integer
s. 第一行包含输入数量,随后的行包含一组
Integer
。
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws java.lang.Exception {
try {
java.io.BufferedReader r = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
int a = Integer.parseInt(r.readLine());
for (int i = 0; i < a; i++) {
StringTokenizer st = new StringTokenizer(r.readLine());
while (st.hasMoreTokens()) {
int first = Integer.parseInt(st.nextToken());
int second = Integer.parseInt(st.nextToken());
if (first < second)
System.out.println("<");
else if (first > second)
System.out.println(">");
else
System.out.println("=");
}
}
} catch (Exception e) {
System.err.print(e.getMessage());
}
}
}
You are performing a lot of redundant autoboxing and outboxing there which could be saved if you define first
and second
as primitive int
s instead of java.lang.Integer
wrapper classes. 您在此处执行了很多多余的自动装箱和发件箱操作,如果您将
first
和second
定义为原始int
而不是java.lang.Integer
包装器类,则可以节省这些冗余。 I doubt, however, that for any reasonable size of input you'd even notice the difference. 但是,我怀疑对于任何合理大小的输入,您是否都会注意到差异。
Here are a few suggestions, I am not sure these will help a lot. 这里有一些建议,我不确定这些建议会有很大帮助。
1) This, 1)这个
if (first < second)
System.out.println("<");
else if (first > second)
System.out.println(">");
else
System.out.println("=");
can be changed to 可以更改为
System.out.println(first < second
? "<"
: first > second
? ">"
: "="
);
2) Since you are using a throws clause and your try-catch
does nothing you can remove it. 2)由于您使用的是throws子句,而
try-catch
执行任何操作,因此可以将其删除。
3) Here, 3)在这里
int first = Integer.parseInt(st.nextToken());
int second = Integer.parseInt(st.nextToken());
You are not checking for hasMoreTokens()
the second time. 您不是第二次检查
hasMoreTokens()
。
4) Use split()
instead of StringTokenizer
. 4)使用
split()
代替StringTokenizer
。 More on that here and here . 在这里和这里更多。
You may consider to use the BufferedReader
's constructor below: 您可以考虑在下面使用
BufferedReader
的构造函数:
public BufferedReader(Reader in, int sz)
Giving the sz
parameter a reasonable high value , can avoid the buffer to be refilled too often. 给
sz
参数一个合理的高值 ,可以避免缓冲区太频繁地重新填充。
As a side note, while readLine()
retuns null
if the end of the stream has been reached, it's better to check its return value before calling new StringTokenizer(r.readLine())
and Integer.parseInt(r.readLine())
. 附带说明一下,如果已到达流的末尾,则
readLine()
返回null
,但最好在调用new StringTokenizer(r.readLine())
和Integer.parseInt(r.readLine())
之前检查其返回值。 。
The edited class follows: 编辑后的类如下:
public class Main {
public static void main(String[] args) {
BufferedReader r;
String line;
StringTokenizer st;
int a, first, second;
r = new BufferedReader(new InputStreamReader(System.in), 4096);
try {
line = r.readLine();
if (line != null) {
a = Integer.parseInt(line);
for (int i = 0; i < a; ++i) {
line = r.readLine();
if (line == null)
break;
st = new StringTokenizer(line);
while (st.hasMoreTokens()) {
first = Integer.parseInt(st.nextToken());
second = Integer.parseInt(st.nextToken());
if (first < second)
System.out.println("<");
else if (first > second)
System.out.println(">");
else
System.out.println("=");
}
}
}
r.close();
} catch (Exception e) {
System.err.print(e.getMessage());
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.