简体   繁体   中英

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. 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.

Is there a way to print to the console faster or do I have to go with the JFrame/JTextArea strategy?

Your problem is that you're printing a 250kb file to the console. No one can read that or make sense of it (reading hex? at 500 lines/sec?), so you're doing a completely unnecessary step.

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.

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. It is still slower that writing to a file, but orders of magnitude than dumping huge amounts of text to the user's display.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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