简体   繁体   English

System.out.print慢

[英]System.out.print slow

I have an application that I made that prints out the bytes of a file to the system console. 我有一个应用程序,可以将文件的字节打印到系统控制台。 (cmd.exe, in my case) It is working very well, but I wanted it to go faster(for large files) so I optimized it. (以我的情况为cmd.exe),它运行得很好,但是我希望它运行得更快(对于大文件),因此我对其进行了优化。 From my own testing, the part where it reads bytes from the file and stores them in a char array takes 15 to 32 milliseconds on a 250,000 byte file, but printing those chars to the command window takes over 9000 milliseconds!(9 seconds, almost 300 times slower!) I have tried http://www.rgagnon.com/javadetails/java-0603.html but I did not see a significant improvement. 从我自己的测试来看,它从文件读取字节并将其存储在char数组中的部分在250,000字节的文件上花费15到32毫秒,但是将这些char打印到命令窗口需要9000毫秒!(9秒,几乎慢了300倍!)我已经尝试了http://www.rgagnon.com/javadetails/java-0603.html,但没有看到明显的改善。

Is there a way to print to the console faster or do I have to go with the JFrame/JTextArea strategy? 有没有一种方法可以更快地打印到控制台,还是我必须采用JFrame / JTextArea策略?

Your problem is that you're printing a 250kb file to the console. 您的问题是您要将250kb文件打印到控制台。 No one can read that or make sense of it (reading hex? at 500 lines/sec?), so you're doing a completely unnecessary step. 没有人能读懂或理解它(以500行/秒的速度读取hex?),因此您正在做一个完全不必要的步骤。

If you're converting a file to a hex character representation, stick the output in a file and let the user view it with a real text viewer instead of dumping it in the console. 如果要将文件转换为十六进制字符表示形式,请将输出粘贴到文件中,并让用户使用真实文本查看器查看,而不是将其转储到控制台中。

Using Swing/awt components will let you make this with your own GUI elements, as you've pointed out. 正如您已经指出的那样,使用Swing / awt组件将使您能够使用自己的GUI元素来进行此操作。

Writing to a console that is displayed on the screen is always going to be a lot slower than writing to a file. 写入屏幕上显示的控制台总是比写入文件要慢得多。 The system has to do a lot of work to render all of those characters to pixels and continually paint / repaint them so that they SCROLL through the console's display area. 系统必须做很多工作才能将所有这些字符渲染为像素,并不断对其进行绘制/重新绘制,以使它们在控制台的显示区域滚动。

The only cure is to not do it. 唯一的办法就是不这样做。

However, if you want a faster alternative that still allows you to view the output, pipe the output to the "less" command or equivalent. 但是,如果您希望有一个更快的替代方法仍然可以查看输出,则将输出通过管道传递给“ less”命令或等效命令。 It is still slower that writing to a file, but orders of magnitude than dumping huge amounts of text to the user's display. 写入文件的速度仍然较慢,但是比将大量文本转储到用户的显示器要大几个数量级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM