如何优化速度/内存,以便用于读取给定输入的最后5000行Java代码。

huangapple go评论62阅读模式
英文:

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&lt;String&gt; deque = new LinkedList&lt;String&gt;(); //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 &gt;= 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&lt;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.

&lt;dependency&gt;
    &lt;groupId&gt;commons-io&lt;/groupId&gt;
    &lt;artifactId&gt;commons-io&lt;/artifactId&gt;
    &lt;version&gt;2.5&lt;/version&gt;
&lt;/dependency&gt;

A simple code like below:

File inputFile = new File(&quot;file name&quot;);
ReversedLinesFileReader reader = new ReversedLinesFileReader(inputFile, StandardCharsets.UTF_8);
Stack&lt;String&gt; lines = new Stack&lt;&gt;();
String lineContent;
int count = 0;
while ((lineContent= reader.readLine()) != null &amp;&amp; count++ &lt; 5000) {
	lines.add(lineContent);
}

while (!lines.isEmpty()) {
	System.out.println(lines.pop());
}

huangapple
  • 本文由 发表于 2020年9月21日 07:38:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63984624.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定