# Ruby and an efficiency

**URL:** <https://rubytalk.org/t/ruby-and-an-efficiency/34963>\
**Category:** ruby-talk\
**Created:** [31 January 2007 13:17 UTC](https://rubytalk.org/t/ruby-and-an-efficiency/34963 "2007-01-31T13:17:15Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Miroslaw\_Maziarz](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@Miroslaw\_Maziarz](https://rubytalk.org/u/Miroslaw_Maziarz)\
**Post date:** [31 January 2007 13:17 UTC](https://rubytalk.org/t/ruby-and-an-efficiency/34963/1 "2007-01-31T13:17:15Z")

</div>

Hi,  
Could anyone tell me or send links where I can find some reliable articles  
about efficiency of ruby.  
How I should coding to keep ruby efficient?

Can I implement c programs in ruby cod?

Greets,  
zirael

---

<div class="post-metadata">

**Author:** ![Jan\_Svitok](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@Jan\_Svitok](https://rubytalk.org/u/Jan_Svitok)\
**Post date:** [31 January 2007 14:19 UTC](https://rubytalk.org/t/ruby-and-an-efficiency/34963/2 "2007-01-31T14:19:30Z")

</div>

General:  
[http://redhanded.hobix.com/inspect/theFullyUpturnedBin.html](http://redhanded.hobix.com/inspect/theFullyUpturnedBin.html)  
[http://groups.google.com/group/comp.lang.ruby/browse\_thread/thread/b1a4bbd4d9ec6246/949cbb0290ac7bef#949cbb0290ac7bef](http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/b1a4bbd4d9ec6246/949cbb0290ac7bef#949cbb0290ac7bef)

ruby-prof:

> **[Profile and ruby-prof](http://on-ruby.blogspot.com/2006/08/profile-and-ruby-prof.html)**
>
> I've been playing with profile (from the Standard Library) and ruby-prof (the profile replacement written by Shugo Maeda and Charlie Savage...

> **[Profile and ruby-prof: Getting Specific](http://on-ruby.blogspot.com/2006/08/profile-and-ruby-prof-getting-specific.html)**
>
> I'll close up my profiling trilogy with a little bit about profiling parts of your Ruby code. While ruby -r profile my\_program or ruby-pro...

> **[ruby-prof and call graphs](http://on-ruby.blogspot.com/2006/08/ruby-prof-and-call-graphs.html)**
>
> Sorry it's been so long since my last post. I've been on vacation for the last week, and Internet access wasn't a big part of it. I did ge...

RubyInline:

> **[RubyInline, Making Making Things Faster Easier](http://on-ruby.blogspot.com/2006/07/rubyinline-making-making-things-faster.html)**
>
> Today on ruby-talk, I caught a thread called "For Performance, Write it in C" . I'd like to present a slight amendment to that thesis. Le...

[http://segment7.net/projects/ruby/inline\_optimization.html](http://segment7.net/projects/ruby/inline_optimization.html)

I guess somewhere on mongrel site (mongrel.rubyforge.org) there is an  
article about tuning performance, unfortunately the site is down, so I  
cannot check it.

> **···**
>
> On 1/31/07, Miroslaw Maziarz \<miroslaw.maziarz@gmail.com\> wrote:
> 
> > Hi,  
> > Could anyone tell me or send links where I can find some reliable articles  
> > about efficiency of ruby.  
> > How I should coding to keep ruby efficient?
> > 
> > Can I implement c programs in ruby cod?
> > 
> > Greets,  
> > zirael

---

<div class="post-metadata">

**Author:** ![John\_Carter](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@John\_Carter](https://rubytalk.org/u/John_Carter)\
**Post date:** [1 February 2007 01:34 UTC](https://rubytalk.org/t/ruby-and-an-efficiency/34963/3 "2007-02-01T01:34:52Z")

</div>

Almost, but not quite, is...

&nbsp;&nbsp;&nbsp;[http://wiki.rubygarden.org/Ruby/page/show/RubyOptimization](http://wiki.rubygarden.org/Ruby/page/show/RubyOptimization)

> **···**
>
> On Wed, 31 Jan 2007, Miroslaw Maziarz wrote:
> 
> > Could anyone tell me or send links where I can find some reliable articles  
> > about efficiency of ruby.  
> > How I should coding to keep ruby efficient?
> > 
> > Can I implement c programs in ruby cod?
> 
> rubyinline.
> 
> John Carter Phone : (64)(3) 358 6639  
> Tait Electronics Fax : (64)(3) 359 4632  
> PO Box 1645 Christchurch Email : john.carter@tait.co.nz  
> New Zealand

---

<div class="post-metadata">

**Author:** ![Guest15](https://avatars.discourse-cdn.com/v4/letter/g/6de8d8/32.png) [@Guest15](https://rubytalk.org/u/Guest15)\
**Post date:** [31 January 2007 14:31 UTC](https://rubytalk.org/t/ruby-and-an-efficiency/34963/4 "2007-01-31T14:31:56Z")

</div>

> RubyInline:  
> [On Ruby: RubyInline, Making Making Things Faster Easier](http://on-ruby.blogspot.com/2006/07/rubyinline-making-making-things-faster.html)

Some people dislike this example because there is so much room for algorithmic  
improvement (which I don't dispute). Another example, and one that uses the  
Ruby C API from RubyInline is here:

> **[RubyInline: Going a bit Further](http://on-ruby.blogspot.com/2006/07/rubyinline-going-bit-further.html)**
>
> Last time around, I wrote about RubyInline and profiling, and raised a couple of questions. This time around I'm going to talk about RubyIn...

> [http://segment7.net/projects/ruby/inline\_optimization.html](http://segment7.net/projects/ruby/inline_optimization.html)

You might also find zenspider's posts interesting:

[http://blog.zenspider.com/archives/2006/08/writing\_c\_exten.html](http://blog.zenspider.com/archives/2006/08/writing_c_exten.html)

[http://blog.zenspider.com/archives/2006/09/recursive\_functions\_in\_rubyinline.html](http://blog.zenspider.com/archives/2006/09/recursive_functions_in_rubyinline.html)

> **···**
>
> On 1/31/07, Jan Svitok \<jan.svitok@gmail.com\> wrote:
> 
> > I guess somewhere on mongrel site (mongrel.rubyforge.org) there is an  
> > article about tuning performance, unfortunately the site is down, so I  
> > cannot check it.
> 
> --  
> thanks,  
> -pate  
> -------------------------
> 
> > **[On Ruby](http://on-ruby.blogspot.com)**
> >
> > The (mostly) tech related musings of Pat Eyler. Ruby, Erlang, Haskell, Scala, Ocaml, Publishing, and more ...

---

<div class="post-metadata">

**Author:** ![Devin\_Mullins](https://avatars.discourse-cdn.com/v4/letter/d/958977/32.png) [@Devin\_Mullins](https://rubytalk.org/u/Devin_Mullins)\
**Post date:** [1 February 2007 01:05 UTC](https://rubytalk.org/t/ruby-and-an-efficiency/34963/5 "2007-02-01T01:05:31Z")

</div>

Jan Svitok wrote:  
\> stuff

Hrm. I was going to add these links to [http://wiki.rubygarden.org/Ruby/page/show/RubyTalkPermaThreads](http://wiki.rubygarden.org/Ruby/page/show/RubyTalkPermaThreads), but it got shoved into the Tarpit ("Banned URL in content"). Help, anybody?

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [31 January 2007 20:55 UTC](https://rubytalk.org/t/ruby-and-an-efficiency/34963/6 "2007-01-31T20:55:10Z")

</div>

This site shows a rather lackluster boost provided by RubyInline.

Let's compare Ruby and Lua.

# A Ruby program based on the one at the link above.  
class Array  
&nbsp;&nbsp;# build the Array#ravg method in Ruby  
&nbsp;&nbsp;def ravg  
&nbsp;&nbsp;&nbsp;&nbsp;Float(self.inject {|sum, elem| sum += elem }) /  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Float(self.length)  
&nbsp;&nbsp;end  
end

time = Time.now

# build a good sized loop over a big array  
max\_loop = (ARGV.shift || 20).to\_i  
max\_size = (ARGV.shift || 1\_000\_000).to\_i  
a = (1..max\_size).to\_a

total = 0  
max\_loop.times {  
&nbsp;&nbsp;total += a.ravg  
}  
p Time.now - time  
p total, total/max\_loop

--- Ruby's output -----  
151.657  
10000010.0  
500000.5

-- A Lua program.  
function average( list )  
&nbsp;&nbsp;local sum = 0  
&nbsp;&nbsp;for i = 1, #list do  
&nbsp;&nbsp;&nbsp;&nbsp;sum = sum + list[i]  
&nbsp;&nbsp;end  
&nbsp;&nbsp;return sum / #list  
end

local max\_loop = 20  
local max\_size = 1e6

time = os.clock()

local a = {}  
for i = 1, max\_size do a[i] = i end

local total = 0  
for i = 1, max\_loop do  
&nbsp;&nbsp;total = total + average( a )  
end  
print( os.clock() - time )  
print( total )  
print( total/max\_loop )

--- output for Lua -----  
1.859  
10000010  
500000.5

--- output for LuaJIT -----  
0.891  
10000010  
500000.5

Based on these results and the ones shown at the link above:

Speed compared to pure Ruby

> **···**
>
> On Jan 31, 8:31 am, "pat eyler" \<pat.ey...@gmail.com\> wrote:
> 
> > On 1/31/07, Jan Svitok \<jan.svi...@gmail.com\> wrote:
> > 
> > \> RubyInline:  
> > \>[http://on-ruby.blogspot.com/2006/07/rubyinline-making-making-things-f\.\.\](http://on-ruby.blogspot.com/2006/07/rubyinline-making-making-things-f%5C.%5C.%5C).
> > 
> > Some people dislike this example because there is so much room for algorithmic  
> > improvement (which I don't dispute). Another example, and one that uses the  
> > Ruby C API from RubyInline is here:
> > 
> > [On Ruby: RubyInline: Going a bit Further](http://on-ruby.blogspot.com/2006/07/rubyinline-going-bit-further.html)
> 
> ---------------------------  
> RubyInline 4.19  
> Lua 81.57  
> LuaJIT 170.21

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [31 January 2007 21:17 UTC](https://rubytalk.org/t/ruby-and-an-efficiency/34963/7 "2007-01-31T21:17:04Z")

</div>

use sledgehammers to drive tacks in you want them in fast:

&nbsp;&nbsp;&nbsp;harp:~ \> cat a.rb  
&nbsp;&nbsp;&nbsp;class Array  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'narray'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def ravg() NArray.to\_na(self).mean end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;time = Time.now

&nbsp;&nbsp;&nbsp;max\_loop = (ARGV.shift || 20).to\_i  
&nbsp;&nbsp;&nbsp;max\_size = (ARGV.shift || 1\_000\_000).to\_i  
&nbsp;&nbsp;&nbsp;a = (1..max\_size).to\_a

&nbsp;&nbsp;&nbsp;total = 0  
&nbsp;&nbsp;&nbsp;max\_loop.times { total += a.ravg }  
&nbsp;&nbsp;&nbsp;p Time.now - time  
&nbsp;&nbsp;&nbsp;p total, total/max\_loop

&nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb  
&nbsp;&nbsp;&nbsp;4.451964  
&nbsp;&nbsp;&nbsp;9998827.52  
&nbsp;&nbsp;&nbsp;499941.376

even better

&nbsp;&nbsp;&nbsp;harp:~ \> cat a.rb  
&nbsp;&nbsp;&nbsp;require 'narray'

&nbsp;&nbsp;&nbsp;time = Time.now

&nbsp;&nbsp;&nbsp;max\_loop = (ARGV.shift || 20).to\_i  
&nbsp;&nbsp;&nbsp;max\_size = (ARGV.shift || 1\_000\_000).to\_i  
&nbsp;&nbsp;&nbsp;( a = NArray.float max\_size ).indgen!

&nbsp;&nbsp;&nbsp;total = 0  
&nbsp;&nbsp;&nbsp;max\_loop.times { total += a.mean }  
&nbsp;&nbsp;&nbsp;p Time.now - time  
&nbsp;&nbsp;&nbsp;p total, total/max\_loop

&nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb  
&nbsp;&nbsp;&nbsp;0.266714  
&nbsp;&nbsp;&nbsp;9999990.0  
&nbsp;&nbsp;&nbsp;499999.5

and this is only a

&nbsp;&nbsp;&nbsp;harp:~ \> grep -i mhz /proc/cpuinfo  
&nbsp;&nbsp;&nbsp;cpu MHz : 2386.616

it compiles on windows and \*nix. matz - can we have it in the core?

😉

-a

> **···**
>
> On Thu, 1 Feb 2007, William James wrote:
> 
> > > [On Ruby: RubyInline: Going a bit Further](http://on-ruby.blogspot.com/2006/07/rubyinline-going-bit-further.html)
> > 
> > This site shows a rather lackluster boost provided by RubyInline.
> > 
> > Let's compare Ruby and Lua.
> > 
> > # A Ruby program based on the one at the link above.  
> > class Array  
> > # build the Array#ravg method in Ruby  
> > def ravg  
> > &nbsp;&nbsp;&nbsp;Float(self.inject {|sum, elem| sum += elem }) /  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Float(self.length)  
> > end  
> > end
> > 
> > time = Time.now
> > 
> > # build a good sized loop over a big array  
> > max\_loop = (ARGV.shift || 20).to\_i  
> > max\_size = (ARGV.shift || 1\_000\_000).to\_i  
> > a = (1..max\_size).to\_a
> > 
> > total = 0  
> > max\_loop.times {  
> > total += a.ravg  
> > }  
> > p Time.now - time  
> > p total, total/max\_loop
> > 
> > --- Ruby's output -----  
> > 151.657  
> > 10000010.0  
> > 500000.5
> 
> --  
> we can deny everything, except that we have the possibility of being better.  
> simply reflect on that.  
> - the dalai lama

---

<div class="post-metadata">

**Author:** ![Jason\_R](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Jason\_R](https://rubytalk.org/u/Jason_R)\
**Post date:** [31 January 2007 21:57 UTC](https://rubytalk.org/t/ruby-and-an-efficiency/34963/8 "2007-01-31T21:57:45Z")

</div>

For performance, use a compiled language?

I like that advice MUCH better.

And I don't like Lua. It feels like a scripting language, while Ruby feels  
like a dynamic programming language.

Jason

> **···**
>
> On 1/31/07, ara.t.howard@noaa.gov \<ara.t.howard@noaa.gov\> wrote:
> 
> > On Thu, 1 Feb 2007, William James wrote:
> > 
> > \>\> [On Ruby: RubyInline: Going a bit Further](http://on-ruby.blogspot.com/2006/07/rubyinline-going-bit-further.html)  
> > \>  
> > \> This site shows a rather lackluster boost provided by RubyInline.  
> > \>  
> > \> Let's compare Ruby and Lua.  
> > \>  
> > \> # A Ruby program based on the one at the link above.  
> > \> class Array  
> > \> # build the Array#ravg method in Ruby  
> > \> def ravg  
> > \> Float(self.inject {|sum, elem| sum += elem }) /  
> > \> Float(self.length)  
> > \> end  
> > \> end  
> > \>  
> > \> time = Time.now  
> > \>  
> > \> # build a good sized loop over a big array  
> > \> max\_loop = (ARGV.shift || 20).to\_i  
> > \> max\_size = (ARGV.shift || 1\_000\_000).to\_i  
> > \> a = (1..max\_size).to\_a  
> > \>  
> > \> total = 0  
> > \> max\_loop.times {  
> > \> total += a.ravg  
> > \> }  
> > \> p Time.now - time  
> > \> p total, total/max\_loop  
> > \>  
> > \> --- Ruby's output -----  
> > \> 151.657  
> > \> 10000010.0  
> > \> 500000.5
> > 
> > use sledgehammers to drive tacks in you want them in fast:
> > 
> > &nbsp;&nbsp;&nbsp;harp:~ \> cat a.rb  
> > &nbsp;&nbsp;&nbsp;class Array  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'narray'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def ravg() NArray.to\_na(self).mean end  
> > &nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;time = Time.now
> > 
> > &nbsp;&nbsp;&nbsp;max\_loop = (ARGV.shift || 20).to\_i  
> > &nbsp;&nbsp;&nbsp;max\_size = (ARGV.shift || 1\_000\_000).to\_i  
> > &nbsp;&nbsp;&nbsp;a = (1..max\_size).to\_a
> > 
> > &nbsp;&nbsp;&nbsp;total = 0  
> > &nbsp;&nbsp;&nbsp;max\_loop.times { total += a.ravg }  
> > &nbsp;&nbsp;&nbsp;p Time.now - time  
> > &nbsp;&nbsp;&nbsp;p total, total/max\_loop
> > 
> > &nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb  
> > &nbsp;&nbsp;&nbsp;4.451964  
> > &nbsp;&nbsp;&nbsp;9998827.52  
> > &nbsp;&nbsp;&nbsp;499941.376
> > 
> > even better
> > 
> > &nbsp;&nbsp;&nbsp;harp:~ \> cat a.rb  
> > &nbsp;&nbsp;&nbsp;require 'narray'
> > 
> > &nbsp;&nbsp;&nbsp;time = Time.now
> > 
> > &nbsp;&nbsp;&nbsp;max\_loop = (ARGV.shift || 20).to\_i  
> > &nbsp;&nbsp;&nbsp;max\_size = (ARGV.shift || 1\_000\_000).to\_i  
> > &nbsp;&nbsp;&nbsp;( a = NArray.float max\_size ).indgen!
> > 
> > &nbsp;&nbsp;&nbsp;total = 0  
> > &nbsp;&nbsp;&nbsp;max\_loop.times { total += a.mean }  
> > &nbsp;&nbsp;&nbsp;p Time.now - time  
> > &nbsp;&nbsp;&nbsp;p total, total/max\_loop
> > 
> > &nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb  
> > &nbsp;&nbsp;&nbsp;0.266714  
> > &nbsp;&nbsp;&nbsp;9999990.0  
> > &nbsp;&nbsp;&nbsp;499999.5
> > 
> > and this is only a
> > 
> > &nbsp;&nbsp;&nbsp;harp:~ \> grep -i mhz /proc/cpuinfo  
> > &nbsp;&nbsp;&nbsp;cpu MHz : 2386.616
> > 
> > it compiles on windows and \*nix. matz - can we have it in the core?
> > 
> > 😉
> > 
> > -a  
> > --  
> > we can deny everything, except that we have the possibility of being  
> > better.  
> > simply reflect on that.  
> > - the dalai lama

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [1 February 2007 17:25 UTC](https://rubytalk.org/t/ruby-and-an-efficiency/34963/9 "2007-02-01T17:25:09Z")

</div>

> \>\>[On Ruby: RubyInline: Going a bit Further](http://on-ruby.blogspot.com/2006/07/rubyinline-going-bit-further.html)
> 
> \> This site shows a rather lackluster boost provided by RubyInline.
> 
> \> Let's compare Ruby and Lua.
> 
> \> # A Ruby program based on the one at the link above.  
> \> class Array  
> \> # build the Array#ravg method in Ruby  
> \> def ravg  
> \> Float(self.inject {|sum, elem| sum += elem }) /  
> \> Float(self.length)  
> \> end  
> \> end
> 
> \> time = Time.now
> 
> \> # build a good sized loop over a big array  
> \> max\_loop = (ARGV.shift || 20).to\_i  
> \> max\_size = (ARGV.shift || 1\_000\_000).to\_i  
> \> a = (1..max\_size).to\_a
> 
> \> total = 0  
> \> max\_loop.times {  
> \> total += a.ravg  
> \> }  
> \> p Time.now - time  
> \> p total, total/max\_loop
> 
> \> --- Ruby's output -----  
> \> 151.657  
> \> 10000010.0  
> \> 500000.5
> 
> use sledgehammers to drive tacks in you want them in fast:
> 
> &nbsp;&nbsp;&nbsp;harp:~ \> cat a.rb  
> &nbsp;&nbsp;&nbsp;class Array  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'narray'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def ravg() NArray.to\_na(self).mean end  
> &nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;time = Time.now
> 
> &nbsp;&nbsp;&nbsp;max\_loop = (ARGV.shift || 20).to\_i  
> &nbsp;&nbsp;&nbsp;max\_size = (ARGV.shift || 1\_000\_000).to\_i  
> &nbsp;&nbsp;&nbsp;a = (1..max\_size).to\_a
> 
> &nbsp;&nbsp;&nbsp;total = 0  
> &nbsp;&nbsp;&nbsp;max\_loop.times { total += a.ravg }  
> &nbsp;&nbsp;&nbsp;p Time.now - time  
> &nbsp;&nbsp;&nbsp;p total, total/max\_loop
> 
> &nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb  
> &nbsp;&nbsp;&nbsp;4.451964  
> &nbsp;&nbsp;&nbsp;9998827.52  
> &nbsp;&nbsp;&nbsp;499941.376

The results are wrong. 500000.5 is the correct average.  
And this is slower than pure interpreted Lua (on a 3.19GHz machine).  
Try it.

--- output for Lua -----  
1.859  
10000010  
500000.5

--- output for LuaJIT -----  
0.891  
10000010  
500000.5

> even better
> 
> &nbsp;&nbsp;&nbsp;harp:~ \> cat a.rb  
> &nbsp;&nbsp;&nbsp;require 'narray'
> 
> &nbsp;&nbsp;&nbsp;time = Time.now
> 
> &nbsp;&nbsp;&nbsp;max\_loop = (ARGV.shift || 20).to\_i  
> &nbsp;&nbsp;&nbsp;max\_size = (ARGV.shift || 1\_000\_000).to\_i  
> &nbsp;&nbsp;&nbsp;( a = NArray.float max\_size ).indgen!
> 
> &nbsp;&nbsp;&nbsp;total = 0  
> &nbsp;&nbsp;&nbsp;max\_loop.times { total += a.mean }  
> &nbsp;&nbsp;&nbsp;p Time.now - time  
> &nbsp;&nbsp;&nbsp;p total, total/max\_loop
> 
> &nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb  
> &nbsp;&nbsp;&nbsp;0.266714  
> &nbsp;&nbsp;&nbsp;9999990.0  
> &nbsp;&nbsp;&nbsp;499999.5
> 
> and this is only a
> 
> &nbsp;&nbsp;&nbsp;harp:~ \> grep -i mhz /proc/cpuinfo  
> &nbsp;&nbsp;&nbsp;cpu MHz : 2386.616

So a compiled, very low level, very crude language  
(i.e., C) using low-precision math can beat Lua.  
Not surprising.

> **···**
>
> On Jan 31, 3:17 pm, ara.t.how...@noaa.gov wrote:
> 
> > On Thu, 1 Feb 2007, William James wrote:

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [31 January 2007 22:14 UTC](https://rubytalk.org/t/ruby-and-an-efficiency/34963/10 "2007-01-31T22:14:16Z")

</div>

> For performance, use a compiled language?
> 
> I like that advice MUCH better.

seriously? you'd have to have serious performance contraints to do better  
that this

> > &nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb  
> > &nbsp;&nbsp;&nbsp;0.266714  
> > &nbsp;&nbsp;&nbsp;9999990.0  
> > &nbsp;&nbsp;&nbsp;499999.5

which took less that 30 seconds to write?

there are plenty time working in a compiled language makes sense, but the list  
of requirements for it to make financial sense is quite long imho - and i do  
write alot of c.

kind regards.

-a

> **···**
>
> On Thu, 1 Feb 2007, Jason Roelofs wrote:  
> --  
> we can deny everything, except that we have the possibility of being better.  
> simply reflect on that.  
> - the dalai lama

---

<div class="post-metadata">

**Author:** ![Andy\_Lester](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@Andy\_Lester](https://rubytalk.org/u/Andy_Lester)\
**Post date:** [31 January 2007 23:50 UTC](https://rubytalk.org/t/ruby-and-an-efficiency/34963/11 "2007-01-31T23:50:06Z")

</div>

Ruby IS compiled, just like Perl. It's just done very quickly.

> **···**
>
> On Jan 31, 2007, at 3:57 PM, Jason Roelofs wrote:
> 
> > For performance, use a compiled language?
> > 
> > I like that advice MUCH better.
> 
> --  
> Andy Lester =\> andy@petdance.com =\> [www.petdance.com](http://www.petdance.com) =\> AIM:petdance

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [1 February 2007 18:05 UTC](https://rubytalk.org/t/ruby-and-an-efficiency/34963/12 "2007-02-01T18:05:09Z")

</div>

> The results are wrong. 500000.5 is the correct average. And this is slower  
> than pure interpreted Lua (on a 3.19GHz machine). Try it.

the results are right, my code was wrong. here's the correct translation:

&nbsp;&nbsp;&nbsp;harp:~ \> cat a.rb  
&nbsp;&nbsp;&nbsp;require 'narray'

&nbsp;&nbsp;&nbsp;time = Time.now

&nbsp;&nbsp;&nbsp;max\_loop = (ARGV.shift || 20).to\_i  
&nbsp;&nbsp;&nbsp;max\_size = (ARGV.shift || 1\_000\_000).to\_i  
&nbsp;&nbsp;&nbsp;a = NArray.float(max\_size).indgen + 1

&nbsp;&nbsp;&nbsp;total = 0  
&nbsp;&nbsp;&nbsp;max\_loop.times { total += a.mean }  
&nbsp;&nbsp;&nbsp;p Time.now - time  
&nbsp;&nbsp;&nbsp;p total, total/max\_loop

&nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb  
&nbsp;&nbsp;&nbsp;0.283084  
&nbsp;&nbsp;&nbsp;10000010.0  
&nbsp;&nbsp;&nbsp;500000.5

> So a compiled, very low level, very crude language  
> (i.e., C) using low-precision math can beat Lua.  
> Not surprising.

well, as i just showed - the math is not low precision and it's correct.  
remember lua, ruby, luajit, and ruby extensions are all \_\_exactly\_\_ the same  
thing, namely very low level very crude c. in all cases the good thing is that  
it's c that someone else has written. the point is that you need to get into c  
to go fast and that there's a variety of ways to do it. installing narray is  
just as good a way to do it as any other.

regards.

-a

> **···**
>
> On Fri, 2 Feb 2007, William James wrote:  
> --  
> we can deny everything, except that we have the possibility of being better.  
> simply reflect on that.  
> - the dalai lama

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [1 February 2007 18:25 UTC](https://rubytalk.org/t/ruby-and-an-efficiency/34963/13 "2007-02-01T18:25:47Z")

</div>

it isn't on my machine - but perhaps i'm doing something wrong? can you show  
it?

&nbsp;&nbsp;&nbsp;harp:~ \> cat a.lua  
&nbsp;&nbsp;&nbsp;function average( list )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local sum = 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for i = 1, #list do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum = sum + list[i]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return sum / #list  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;local max\_loop = 20  
&nbsp;&nbsp;&nbsp;local max\_size = 1e6

&nbsp;&nbsp;&nbsp;time = os.clock()

&nbsp;&nbsp;&nbsp;local a = {}  
&nbsp;&nbsp;&nbsp;for i = 1, max\_size do a[i] = i end

&nbsp;&nbsp;&nbsp;local total = 0  
&nbsp;&nbsp;&nbsp;for i = 1, max\_loop do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;total = total + average( a )  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;print( os.clock() - time )  
&nbsp;&nbsp;&nbsp;print( total )  
&nbsp;&nbsp;&nbsp;print( total/max\_loop )

&nbsp;&nbsp;&nbsp;harp:~ \> lua a.lua  
&nbsp;&nbsp;&nbsp;2.44  
&nbsp;&nbsp;&nbsp;10000010  
&nbsp;&nbsp;&nbsp;500000.5

&nbsp;&nbsp;&nbsp;harp:~ \> luac a.lua && lua luac.out  
&nbsp;&nbsp;&nbsp;2.47  
&nbsp;&nbsp;&nbsp;10000010  
&nbsp;&nbsp;&nbsp;500000.5

&nbsp;&nbsp;&nbsp;harp:~ \> cat a.rb  
&nbsp;&nbsp;&nbsp;require 'narray'

&nbsp;&nbsp;&nbsp;time = Time.now

&nbsp;&nbsp;&nbsp;max\_loop = (ARGV.shift || 20).to\_i  
&nbsp;&nbsp;&nbsp;max\_size = (ARGV.shift || 1\_000\_000).to\_i  
&nbsp;&nbsp;&nbsp;a = NArray.float(max\_size).indgen + 1

&nbsp;&nbsp;&nbsp;total = 0  
&nbsp;&nbsp;&nbsp;max\_loop.times { total += a.mean }  
&nbsp;&nbsp;&nbsp;p Time.now - time  
&nbsp;&nbsp;&nbsp;p total, total/max\_loop

&nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb  
&nbsp;&nbsp;&nbsp;0.287334  
&nbsp;&nbsp;&nbsp;10000010.0  
&nbsp;&nbsp;&nbsp;500000.5

regards.

-a

> **···**
>
> On Fri, 2 Feb 2007, William James wrote:
> 
> > And this is slower than pure interpreted Lua (on a 3.19GHz machine). Try  
> > it.
> 
> --  
> we can deny everything, except that we have the possibility of being better.  
> simply reflect on that.  
> - the dalai lama

---

<div class="post-metadata">

**Author:** ![Jason\_R](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Jason\_R](https://rubytalk.org/u/Jason_R)\
**Post date:** [31 January 2007 22:20 UTC](https://rubytalk.org/t/ruby-and-an-efficiency/34963/14 "2007-01-31T22:20:06Z")

</div>

Eh, just being facetious, and narray is a C compiled extension so  
vis-a-vis...

Jason

> **···**
>
> On 1/31/07, ara.t.howard@noaa.gov \<ara.t.howard@noaa.gov\> wrote:
> 
> > On Thu, 1 Feb 2007, Jason Roelofs wrote:
> > 
> > \> For performance, use a compiled language?  
> > \>  
> > \> I like that advice MUCH better.
> > 
> > seriously? you'd have to have serious performance contraints to do better  
> > that this
> > 
> > \>\> harp:~ \> ruby a.rb  
> > \>\> 0.266714  
> > \>\> 9999990.0  
> > \>\> 499999.5
> > 
> > which took less that 30 seconds to write?
> > 
> > there are plenty time working in a compiled language makes sense, but the  
> > list  
> > of requirements for it to make financial sense is quite long imho - and i  
> > do  
> > write alot of c.
> > 
> > kind regards.
> > 
> > -a  
> > --  
> > we can deny everything, except that we have the possibility of being  
> > better.  
> > simply reflect on that.  
> > - the dalai lama

---

<div class="post-metadata">

**Author:** ![Paul\_Brannan](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@Paul\_Brannan](https://rubytalk.org/u/Paul_Brannan)\
**Post date:** [1 February 2007 15:24 UTC](https://rubytalk.org/t/ruby-and-an-efficiency/34963/15 "2007-02-01T15:24:35Z")

</div>

Ruby traverses an AST to evaluate your code. It is not compiled to  
machine code.

Paul

> **···**
>
> On Thu, Feb 01, 2007 at 08:50:06AM +0900, Andy Lester wrote:
> 
> > Ruby IS compiled, just like Perl. It's just done very quickly.

---

<div class="post-metadata">

**Author:** ![PA1](https://avatars.discourse-cdn.com/v4/letter/p/71c47a/32.png) [@PA1](https://rubytalk.org/u/PA1)\
**Post date:** [1 February 2007 18:32 UTC](https://rubytalk.org/t/ruby-and-an-efficiency/34963/16 "2007-02-01T18:32:44Z")

</div>

Not to beat a dead horse, but...

[http://swik.net/Ruby/RedHanded/Mongrel’s+Going+to+Kill+Webrick,+Give+It+a+Month/b176](http://swik.net/Ruby/RedHanded/Mongrel's+Going+to+Kill+Webrick,+Give+It+a+Month/b176)

> **···**
>
> On Feb 01, 2007, at 19:05, ara.t.howard@noaa.gov wrote:
> 
> > the results are right, my code was wrong. here's the correct translation:

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [31 January 2007 22:30 UTC](https://rubytalk.org/t/ruby-and-an-efficiency/34963/17 "2007-01-31T22:30:34Z")

</div>

gotcha. still, \_\_ruby\_\_ is a compiled C extension 😉

-a

> **···**
>
> On Thu, 1 Feb 2007, Jason Roelofs wrote:
> 
> > Eh, just being facetious, and narray is a C compiled extension so  
> > vis-a-vis...
> 
> --  
> we can deny everything, except that we have the possibility of being better.  
> simply reflect on that.  
> - the dalai lama

---

<div class="post-metadata">

**Author:** ![Andy\_Lester](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@Andy\_Lester](https://rubytalk.org/u/Andy_Lester)\
**Post date:** [1 February 2007 15:26 UTC](https://rubytalk.org/t/ruby-and-an-efficiency/34963/18 "2007-02-01T15:26:36Z")

</div>

I understand that. Close enough for 99% of purposes.

> **···**
>
> On Feb 1, 2007, at 9:24 AM, Paul Brannan wrote:
> 
> > On Thu, Feb 01, 2007 at 08:50:06AM +0900, Andy Lester wrote:
> > 
> > > Ruby IS compiled, just like Perl. It's just done very quickly.
> > 
> > Ruby traverses an AST to evaluate your code. It is not compiled to  
> > machine code.
> 
> --  
> Andy Lester =\> andy@petdance.com =\> [www.petdance.com](http://www.petdance.com) =\> AIM:petdance

---

<div class="post-metadata">

**Author:** ![Jason\_R](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Jason\_R](https://rubytalk.org/u/Jason_R)\
**Post date:** [1 February 2007 13:52 UTC](https://rubytalk.org/t/ruby-and-an-efficiency/34963/19 "2007-02-01T13:52:37Z")

</div>

Now that's just pushing it. Perl, Ruby, Python, Lua, Squirrel, are all  
WRITTEN in C, but they don't run at C speeds. There's still the  
interpretation overhead...

And Andy? you're also pushing it. Compiled into machine code vs compiled  
into an interpreted byte code. You know what is being talked about here.

Jason

> **···**
>
> On 1/31/07, ara.t.howard@noaa.gov \<ara.t.howard@noaa.gov\> wrote:
> 
> > On Thu, 1 Feb 2007, Jason Roelofs wrote:
> > 
> > \> Eh, just being facetious, and narray is a C compiled extension so  
> > \> vis-a-vis...
> > 
> > gotcha. still, \_\_ruby\_\_ is a compiled C extension 😉
> > 
> > -a  
> > --  
> > we can deny everything, except that we have the possibility of being  
> > better.  
> > simply reflect on that.  
> > - the dalai lama

---

<div class="post-metadata">

**Author:** ![Andy\_Lester](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@Andy\_Lester](https://rubytalk.org/u/Andy_Lester)\
**Post date:** [1 February 2007 13:58 UTC](https://rubytalk.org/t/ruby-and-an-efficiency/34963/20 "2007-02-01T13:58:54Z")

</div>

> Now that's just pushing it. Perl, Ruby, Python, Lua, Squirrel, are all  
> WRITTEN in C, but they don't run at C speeds. There's still the  
> interpretation overhead...
> 
> And Andy? you're also pushing it. Compiled into machine code vs compiled  
> into an interpreted byte code. You know what is being talked about here.

Wow, so angry.

> **···**
>
> --  
> Andy Lester =\> andy@petdance.com =\> [www.petdance.com](http://www.petdance.com) =\> AIM:petdance

[Next page](https://rubytalk.org/t/ruby-and-an-efficiency/34963.md?page=2)
