Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Has anyone else seen this behavior?

I created a simple Ruby script for giggles to see how fast it would count up on a couple machines I have. Here's the source:

    for n in 1...1000000 do
        puts "#{n}"
end

I ran the script using "time ruby ./rubytest.rb"

On my not-newest-generation MacBook, running this locally took about 15 seconds.

If I SSH from work to my MacBook and run it over that SSH session (over the Internet), it takes nearly 55 seconds.

If I use "ssh localhost" on my MacBook, from the local console, running the script takes about 10 seconds.

Questions: Do Ruby scripts somehow get blocked when dumping output to a console, so it is slowed down when pumping data to a remote terminal? Does SSH compress a connection, so that when I SSH'd in locally the script was faster because it was compressing the display of data fast enough that even with the compression overhead the script could run faster because it was somehow acknowledging the output faster?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

It's because you're echoing all the numbers out to the screen via the puts. These then have to be sent over the SSH connection which extends the amount of time the scripts takes to run. The output is also buffered locally but the effects aren't as noticeable.

You can confirm this by dumping all the output to a file instead.

Examples

local on laptop to screen

$ time ruby r.rb

real    0m19.474s
user    0m3.236s
sys     0m3.207s

local on laptop to file

$ time ruby r.rb >& rb_local.txt

real    0m0.785s
user    0m0.760s
sys     0m0.020s

SSH to desktop to screen

$ time ruby r.rb
real    0m18.026s
user    0m4.118s
sys     0m3.815s

SSH to desktop to file

$ time ruby r.rb >& rb_remote.txt

real    0m3.942s
user    0m3.036s
sys     0m0.741s

When writing scripts such as this its generally a good idea to dump the contents to a file instead and then use a tool such as tail to watch the file periodically.

NOTE: The notation >& will redirect both STDERR and STDOUT to the .txt files. However the output from time will still get displayed to the console. This notation is equivalent to cmd > some_file.txt 2>&1.

share|improve this answer

Printing to a console (especially a remote console) is a fairly expensive operation if done in a tight loop that's executed frequently. Most Ruby I/O is blocking, so it will drastically slow down the execution of the program.

You should get a much more reasonable result if you send the output to a file (or /dev/null, if you don't care about it) instead:

time ruby ./rubytest.rb >/dev/null
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.