I often run Ruby scripts with a couple of lines of puts
every iteration to see what my program is doing while I'm running. Many of these scripts are very time-intensive and take several minutes to complete.
I'm wonder if outputting ('printing') to the command line is slowing down the completion of these scripts, and if so, how much.
I'm not very bothered if it's only a 5% slowdown, but anything more is worth knowing about before I use my puts
so liberally.
There are a lot of variables that will alter your answer. The Ruby version, your compiler, your STDLib, your CPU and even esoteric things like locale settings can have a big impact on
puts
.The only answer is to do some benchmarking. If you are on a Unix system, you can start with something as simple as
then comment out the
puts
and run it again, comparing the answers.If you have a specific block of code that you need to benchmark, you can alternatively use the Benchmark module that comes with Ruby.