I was reading www.ruby-lang.org, and under “To Ruby From C and C++” it
said
- As of Ruby 1.8, code is interpreted at run-time rather than compiled
to any sort of machine- or byte-code.
which sounds like a huge disadvantage compared to other languages. I
did some testing and I get the impression that Ruby is compiling your
code to some form of compiled version in ram before it runs it.
I made the following test programs
ten_million.rb:
#!/usr/bin/ruby -w
i = 10_000_000
while i > 0
i -= 1
end
which ran in 4.9 seconds in ruby 1.8.7 on a 64 bit amd cpu
ten_million.sh
:
i=10000000
while [ $i -gt 0 ] ; do
i=$(($i - 1))
done
which ran in 287 seconds. Note on my machine /bin/sh is a symlink to
/bin/dash
ten_million.pl
#!/usr/bin/perl -w
$i = 10000000;
while ($i > 0) {
$i -= 1;
}
which ran in 2.45 seconds in perl v6.10.0
ten_million.py
#!/usr/bin/python
i = 10000000
while i > 0:
i -= 1
which ran in 2.84 seconds on python 2.6
If Ruby wasn’t being compiled, I think the run time would be comparable
to Bourne shell. I suspect that what they meant by that statement in
the doc is that there is no way to SAVE the compiled version, but the
compiled version does exist in ram.
I can assure you that ruby is not compiled (nor are python or perl).
The reason bash takes so much longer for a seemingly similar
computation is that bash variables are untyped – everything is stored
as a character string. This means that for each step where in ruby you
simply check and decrement a value, bash must parse a string to an
integer, check if it’s greater than zero, parse the same string as an
integer again, subtract one from it, then convert that integer to a
string.
If Ruby wasn’t being compiled, I think the run time would be comparable
to Bourne shell. I suspect that what they meant by that statement in
the doc is that there is no way to SAVE the compiled version, but the
compiled version does exist in ram.
Burke
burke wrote:
I can assure you that ruby is not compiled (nor are python or perl).
Python can be compiled to bytecode. Ruby can’t, unless you’re using
JRuby.
Best,
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
On Jan 26, 11:02 pm, Marnen Laibow-Koser [email protected] wrote:
burke wrote:
I can assure you that ruby is not compiled (nor are python or perl).
Python can be compiled to bytecode. Ruby can’t
Ruby 1.9, running on YARV, compiles into bytecode.
pharrington wrote:
On Jan 26, 11:02�pm, Marnen Laibow-Koser [email protected] wrote:
burke wrote:
I can assure you that ruby is not compiled (nor are python or perl).
Python can be compiled to bytecode. �Ruby can’t
Ruby 1.9, running on YARV, compiles into bytecode.
Aaugh! I knew that.
Best,
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
On Jan 26, 10:37 pm, burke [email protected] wrote:
to Bourne shell. I suspect that what they meant by that statement in
the doc is that there is no way to SAVE the compiled version, but the
compiled version does exist in ram.
Burke
Python’s definitely compiled into bytecode. Perl is also compiled at
runtime.
On Tue, Jan 26, 2010 at 6:20 PM, lalawawa [email protected] wrote:
I was reading www.ruby-lang.org, and under “To Ruby From C and C++” it said
- As of Ruby 1.8, code is interpreted at run-time rather than compiled to
any sort of machine- or byte-code.
which sounds like a huge disadvantage compared to other languages. I did
some testing and I get the impression that Ruby is compiling your code to
some form of compiled version in ram before it runs it.
In the main Ruby 1.8 C implementation, it is not compiled. It parses
the code into an abstract syntax tree, and then the interpreter
executes the code based on that AST. In the main C 1.9.x
implementation, one’s code is compiled to a byte code format.
I made the following test programs
ten_million.rb:
#!/usr/bin/ruby -w
i = 10_000_000
while i > 0
i -= 1
end
Idiom is important.
A do nothing while loop:
Benchmark.bm {|bm| bm.report {i = 10000000; while i > 0; i -= 1; end}}
user system total real
1.840000 0.000000 1.840000 ( 1.841140)
A faster way, in Ruby:
Benchmark.bm {|bm| bm.report {10000000.downto(1) {}}}
user system total real
0.490000 0.000000 0.490000 ( 0.492997)
And it’s still faster, even when we make sure the value of i is
available in the block, so it’s really quite equivalent to the while
loop:
Benchmark.bm {|bm| bm.report {10000000.downto(1) {|i|}}}
user system total real
0.710000 0.000000 0.710000 ( 0.707342)
In general, the MRI Ruby (the C implementation available from
ruby-lang.org) interpreter for version 1.8.x will benchmark slower
than Perl or Python on microbenchmarks, but as you can see above,
choosing the right idiom for implementation can make a large
difference.
Kirk H.
$i = 10000000;
while ($i > 0) {
$i -= 1;
}
A test with simple loop is meaningless.
This is my result (with ruby-1.9):
$ cat runperfor.rb
i = 10_000_000
while i > 0
i -= 1
end
$ cat runperfor.pl
$i = 10_000_000;
while ($i > 0) {
$i–;
}
$ time ruby runperfor.rb
real 0m1.022s
user 0m0.992s
sys 0m0.000s
$ time perl runperfor.pl
real 0m1.652s
user 0m1.600s
sys 0m0.008s
pharrington wrote:
On Jan 26, 11:02 pm, Marnen Laibow-Koser [email protected] wrote:
burke wrote:
I can assure you that ruby is not compiled (nor are python or perl).
Python can be compiled to bytecode. Ruby can’t
Ruby 1.9, running on YARV, compiles into bytecode.
I don’t know what YARV is, but I upgraded my ruby from 1.8 to 1.9, and
the benchmark sped up from 4.9s to 1.4s.
在 2010-01-27三的 13:05 +0900,pharrington写é“:
Perl is also compiled at
runtime.
No. Perl is compiled at compile-time except some special cases like
eval’ing a string.
YARV = Yet Another Ruby VM – the standard VM implementation in Ruby
1.9.