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.

2

There are 2 answers

1
Anko On

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

time ruby program.rb

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.

0
sawa On

From my experience, continuously printing to an opened terminal or shell-mode on a text editor significantly slows down code execution, especially when code highlighting is applied to the output. A good way to deal with this is to output to a log file, and read that file when needed.