Reputation: 99
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)
Upvotes: 1
Views: 87
Reputation: 106027
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):
"a"
onto the top of the stack."b"
onto the top of 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.
Upvotes: 3
Reputation: 1691
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.
Upvotes: 1
Reputation: 4970
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.
Upvotes: 1