英文:
How to optimize speed/memory for java code that is supposed to read the last 5000 lines of a given input
问题
Deque<String> deque = new LinkedList<String>(); // 创建双端队列
int temp = 0; // 输入计数器
for (String line = r.readLine(); line != null; line = r.readLine()) {
deque.addLast(line); // 读取输入并将其放入双端队列
temp++; // 计数器加一
if (temp >= 5000) { // 如果计数器超过5000,开始删除队首元素
deque.removeFirst(); // 这样我们可以保持在5000行输出标记上
}
}
int s = deque.size(); // 占位符,以减少对deque.size的访问次数
for (int i = 0; i < s; i++) { // 循环 -- 输出双端队列内容
w.println(deque.pop());
}
英文:
I'm writing a code that outputs only the last 5000 lines of an input with an appropriate runtime (if input is less than 5000 lines, then it outputs it all) -- unfortunately, my current solutions are too slow at the moment.
I'm pretty sure I've used up all the "list"-types from queues to stacks to arrayLists, and now to dequeues; but I always get the same execution time on my sample which is pretty slow.
This is what I came up with, let me know of any possible improvements I can do or different angles I can take with it.
Deque<String> deque = new LinkedList<String>(); //makes deque
int temp = 0; //counter for input,
for (String line = r.readLine(); line != null; line = r.readLine()) {
deque.addLast(line); //reads input and puts it into the deque
temp++; //counter increases by one
if(temp >= 5000){ //if the counter gets over 5000, we start chopping off the head
deque.removeFirst(); //so we can effectively stay on the 5000 lines output mark
}
}
int s = deque.size(); //placeholder so as to not access deque.size often
for(int i = 0; i<s; i++){ //for loop -- prints out the deque
w.println(deque.pop());
}
答案1
得分: 1
尝试使用apache commons-io库以倒序读取文件。
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
一个简单的代码示例如下:
File inputFile = new File("文件名");
ReversedLinesFileReader reader = new ReversedLinesFileReader(inputFile, StandardCharsets.UTF_8);
Stack<String> lines = new Stack<>();
String lineContent;
int count = 0;
while ((lineContent = reader.readLine()) != null && count++ < 5000) {
lines.add(lineContent);
}
while (!lines.isEmpty()) {
System.out.println(lines.pop());
}
英文:
Try use apache commons-io lib to read file in reverse order.
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
A simple code like below:
File inputFile = new File("file name");
ReversedLinesFileReader reader = new ReversedLinesFileReader(inputFile, StandardCharsets.UTF_8);
Stack<String> lines = new Stack<>();
String lineContent;
int count = 0;
while ((lineContent= reader.readLine()) != null && count++ < 5000) {
lines.add(lineContent);
}
while (!lines.isEmpty()) {
System.out.println(lines.pop());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论