Blogger Templates

Wednesday, March 13, 2013


Ruby sucks at scripting!

Posted in
Scripting languages should launch quickly, run at acceptable speeds, then exit quickly. Consider this script:

#!/usr/bin/env zsh

cat << SNOBOL4 > bogobench.sno
    OUTPUT = "Hello, world!"

cat << REXX > bogobench.rexx
say "Hello, world!"

cat << AWK > bogobench.awk
    print "Hello, world!\n"

cat << RUBY > bogobench.rb
puts "Hello, world!"

cat << LUA > bogobench.lua
print "Hello, world!"

cat << PROLOG >
main :- write('Hello, world!'), nl.

cat << ERLANG > bogobench.escript
main(_) -> io:format("Hello, world!~n").

cat << JAVA >
public class bogobench
    public static void main(String[] args)
        System.out.println("Hello, world!");

/usr/bin/time -p ./bogoloop awk -f bogobench.awk
/usr/bin/time -p ./bogoloop rexx bogobench.rexx
/usr/bin/time -p ./bogoloop snobol4 -b bogobench.sno
/usr/bin/time -p ./bogoloop lua bogobench.lua
/usr/bin/time -p ./bogoloop swipl -q -t main -f
/usr/bin/time -p ./bogoloop java bogobench
/usr/bin/time -p ./bogoloop escript bogobench.escript
/usr/bin/time -p ./bogoloop ruby bogobench.rb

rm bogobench.*

It’s simple enough: it generates the test scripts using here-documents. In the case of Java, placed as an example of a well-known start-up time hog, the test script is then compiled. The script is then run 100 times in a tight loop using this script:

#!/usr/bin/env zsh


echo "Running '$*' $max_iter times…"
while [ $a -lt "$max_iter" ]
    a=$((a + 1))
    $* > /dev/null
echo "…done."

Very straightforward. And, of course, we’d expect a few things:

Execution time should be negligible. All that’s being done, after all, is outputting a string, something scripting languages should excel at.
SWI-Prolog and Erlang both should be pretty damned slow, given that they’re massive languages with massive run-time systems behind them (and in both cases the “scripting” is mostly an afterthought).
Java, with its notorious JVM load time, should be incredibly slow.
The two numbers that are meaningful in the time output are the user and sys values. Take a look at the raw results:

Running 'awk -f bogobench.awk' 100 times…
real 0.66
user 0.03
sys 0.03
Running 'rexx bogobench.rexx' 100 times…
real 0.36
user 0.00
sys 0.05
Running 'snobol4 -b bogobench.sno' 100 times…
real 0.52
user 0.00
sys 0.05
Running 'lua bogobench.lua' 100 times…
real 0.28
user 0.01
sys 0.03
Running 'swipl -q -t main -f' 100 times…
real 2.40
user 1.32
sys 0.70
Running 'java bogobench' 100 times…
real 12.03
user 8.75
sys 2.00
Running 'escript bogobench.escript' 100 times…
real 18.89
user 13.77
sys 2.46
Running 'ruby bogobench.rb' 100 times…
real 49.53
user 43.10
sys 3.74

Guh… what?!

In EVERY SINGLE METRIC Ruby came in slower—often by two orders of magnitude!—than EVERY OTHER LANGUAGE. Let’s consolidate the results, adding together user and system time:

language time(s)
lua 0.04
rexx 0.05
snobol4 0.05
awk 0.06
prolog 2.02
java 10.75
erlang 16.23
ruby 46.84
Consider what this means. Lua is a better scripting language than Ruby in that it is just the fastest of the bunch while being easily as powerful. Rexx, a language made in the ‘70s on ancient, creaking mainframes is a better scripting language than Ruby in that, while it may be a little bit less powerful, it sure as Hell is faster at loading. (And how much power do you actually need for scripting?) Awk, too, another language from the ‘70s, is more than powerful enough for most scripting needs, and well within acceptable load speed parameters.

More embarrassingly, Prolog, a language not intended for scripting, and, further, an implementation that’s not noted for being quick, is almost acceptable as a scripting language, coming in a good twenty times as fast at loading as Ruby. Erlang, too, even though it’s a painfully slow startup, is over three times as fast as Ruby at loading.

Even Java, a language not intended for scripting and infamously slow at loading is almost five times as fast as Ruby (and about 1.6 times as fast as Erlang).

Ruby, it turns out, is worse as a scripting language than three scripting languages from as far back as the ‘60s, not to mention three languages that aren’t even really intended for scripting.

Source: Hacker News