问题
What is the effect of SYSO - System.out.println()
? If my computer is executing a large job such as fetching 2 Lakhs (approximately 2,000,000) records from a database to a file, does SYSO affect the execution time?
回答1:
Of course it affects it. Every operation comes with a cost. Writing to system out is IO, and comes with a non-negligible cost. To know the exact effect on your program, there's no other solution than benchmarking: run the program with and without the println calls, and measure the time it takes for both versions, or use a profiler.
You should use a logging framework (like slf4j, log4j or java util logging), which would allow for various kinds of outputs (sysout, file, etc.), and which would also allow deactivating the output log by just changing some property in a config file.
回答2:
The effect on performance is very much dependent on how fast the recipient (usually a console or a file system) can read from the stream and how big of a buffer there is. For example, actually looking at the console, as the program is printing, usually makes it really slow, but when you minimize the console window it's faster.
来源:https://stackoverflow.com/questions/10966984/what-is-effect-of-syso-system-out-println