1

I was going through the arrays on Ruby, when i came across a method of creating an array (there may be more).

arr1 = %w(first second third) # => ["first", "second", "third"]

I was wondering, that since Ruby's own methods reduce so much boilerplate, they might actually be slow. But given the fact that Ruby is written in C, does it have some considerable impact on speed/performance? (in a program where there are many such statements)

Faraaz Ahmad
  • 99
  • 2
  • 7
  • Profile it and determine if it does. The likely answer is "it doesn't", but that genuinely depends on your usage of it and how often these arrays are generated. – Makoto May 25 '16 at 16:36
  • is there a tool to "compare" it, other than creating separate programs and/or using irb – Faraaz Ahmad May 25 '16 at 16:38
  • 3
    As Ruby prepares to run the script it will process `%w(...)` and `[...]` array assignments. It doesn't do it when evaluating the script, so they won't affect the speed. This is similar to whether single-quote strings are faster than double-quote strings, they're the same or so close it's not an issue. Also, define "slow", as speed is relative to many things. Ruby, even on a "slow" host, is usually plenty fast to do whatever we need as it's waiting on databases, other hosts, and users. – the Tin Man May 25 '16 at 18:31

3 Answers3

3

As @theTinMan alluded to in the comments, you must understand the distinction between the language (syntax) and the logic (semantics). For example, suppose someone asked you to write a program that prints the number 1,000. You'd probably write it like this:

puts 1000

But you could also write any of these:

  • puts 1_000
  • puts 0b1111101000
  • puts 01750

These are all the same. Not "the same" as in they produce the same results, but "the same" as in Ruby parses and executes them exactly the same way. Their syntaxes are different but their semantics are identical.

The same is true for Ruby's different array syntaxes (and for its equivalent string syntaxes, Regexp literals, etc.). You can test this yourself using Ruby's --dump insns (dump instruction sequence) option:

$ ruby --dump insns -e 'arr = ["a", "b"]'
== disasm: <RubyVM::InstructionSequence:<main>@-e>======================
local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 2] arr
0000 trace            1                                               (   1)
0002 putstring        "a"
0004 putstring        "b"
0006 newarray         2
0008 dup
0009 setlocal_OP__WC__0 2
0011 leave
$ ruby --dump insns -e 'arr = %w(a b)'
== disasm: <RubyVM::InstructionSequence:<main>@-e>======================
local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 2] arr
0000 trace            1                                               (   1)
0002 putstring        "a"
0004 putstring        "b"
0006 newarray         2
0008 dup
0009 setlocal_OP__WC__0 2
0011 leave

Completely identical. The salient instructions, of course, are 0002–0006:

0002 putstring        "a"
0004 putstring        "b"
0006 newarray         2

These instructions say (more or less):

  1. (0002) Push the string "a" onto the top of the stack.
  2. (0004) Push the string "b" onto the top of the stack.
  3. (0006) Pop the top two values from the stack, make an array from them and push it onto the stack.

These are the actual instructions that the MRI VM will execute in both cases. Ruby never knows that you used %w( ... ) instead of [ ... ] and there's no additional code it has to execute.

Jordan Running
  • 102,619
  • 17
  • 182
  • 182
  • Sites that compare what kind of stuff? – Jordan Running May 25 '16 at 22:13
  • Really useful answer! I really appreciate answering with facts (unlike school teachers). Could you please provide a link to some site(s) that do teach such stuff? also does it mean that ruby's relatively slow performance is correctly explained here ? [link](http://stackoverflow.com/questions/5168718/what-blocks-ruby-python-to-get-javascript-v8-speed) – Faraaz Ahmad May 25 '16 at 22:20
  • 2
    @Jordan Great answer! Faraaz, if you want to go deeply into this, the best resource I know of is the book "Ruby Under A Microscope". – Keith Bennett May 26 '16 at 05:10
  • @KeithBennett Thanks. Really interesting book – Faraaz Ahmad May 29 '16 at 08:54
1

Some of MRI Ruby is coded in C, and the rest of it in Ruby. Some of JRuby is coded in Java, and the rest of it in Ruby.

Ruby is an awesome language, but it can be slower than some other languages, especially lower level languages like Java and C.

Ruby's strength is not runtime performance, but rather the clarity, expressiveness, conciseness, and speed of development.

There is no one answer to the question, since each situation's calculation of cost and benefit has its own inputs, its own weights for the relative importance of each alternative's strengths; and its own mix of code that may or may not be subject to Ruby's slower speed. Of course, you would need to benchmark your code to see whether or not the difference was even noticeable -- it may not be.

I (and I think most or all Rubyists) believe that in most cases the difference in speed is not great enough or important enough to motivate us to abandon Ruby in favor of another language.

Keith Bennett
  • 4,722
  • 1
  • 25
  • 35
1

I'll just add, if you want to run some tests to compare the speeds you should look at the Benchmark module: http://ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html.

lwassink
  • 1,595
  • 13
  • 16