Ruby and an efficiency

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

General:
http://redhanded.hobix.com/inspect/theFullyUpturnedBin.html
http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/b1a4bbd4d9ec6246/949cbb0290ac7bef#949cbb0290ac7bef

ruby-prof:

RubyInline:

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

Almost, but not quite, is...

   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

RubyInline:
On Ruby: RubyInline, Making Making Things Faster Easier

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:

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/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
-------------------------

Jan Svitok wrote:
> stuff

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

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

-- A Lua program.
function average( list )
  local sum = 0
  for i = 1, #list do
    sum = sum + list[i]
  end
  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
  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\.\.\.

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

---------------------------
RubyInline 4.19
Lua 81.57
LuaJIT 170.21

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

   harp:~ > cat a.rb
   class Array
     require 'narray'
     def ravg() NArray.to_na(self).mean end
   end

   time = Time.now

   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

   harp:~ > ruby a.rb
   4.451964
   9998827.52
   499941.376

even better

   harp:~ > cat a.rb
   require 'narray'

   time = Time.now

   max_loop = (ARGV.shift || 20).to_i
   max_size = (ARGV.shift || 1_000_000).to_i
   ( a = NArray.float max_size ).indgen!

   total = 0
   max_loop.times { total += a.mean }
   p Time.now - time
   p total, total/max_loop

   harp:~ > ruby a.rb
   0.266714
   9999990.0
   499999.5

and this is only a

   harp:~ > grep -i mhz /proc/cpuinfo
   cpu MHz : 2386.616

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

:wink:

-a

···

On Thu, 1 Feb 2007, William James wrote:

On Ruby: RubyInline: Going a bit Further

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

--
we can deny everything, except that we have the possibility of being better.
simply reflect on that.
- the dalai lama

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
>
> 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:

   harp:~ > cat a.rb
   class Array
     require 'narray'
     def ravg() NArray.to_na(self).mean end
   end

   time = Time.now

   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

   harp:~ > ruby a.rb
   4.451964
   9998827.52
   499941.376

even better

   harp:~ > cat a.rb
   require 'narray'

   time = Time.now

   max_loop = (ARGV.shift || 20).to_i
   max_size = (ARGV.shift || 1_000_000).to_i
   ( a = NArray.float max_size ).indgen!

   total = 0
   max_loop.times { total += a.mean }
   p Time.now - time
   p total, total/max_loop

   harp:~ > ruby a.rb
   0.266714
   9999990.0
   499999.5

and this is only a

   harp:~ > grep -i mhz /proc/cpuinfo
   cpu MHz : 2386.616

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

:wink:

-a
--
we can deny everything, except that we have the possibility of being
better.
simply reflect on that.
- the dalai lama

>>On Ruby: RubyInline: Going a bit Further

> 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:

   harp:~ > cat a.rb
   class Array
     require 'narray'
     def ravg() NArray.to_na(self).mean end
   end

   time = Time.now

   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

   harp:~ > ruby a.rb
   4.451964
   9998827.52
   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

   harp:~ > cat a.rb
   require 'narray'

   time = Time.now

   max_loop = (ARGV.shift || 20).to_i
   max_size = (ARGV.shift || 1_000_000).to_i
   ( a = NArray.float max_size ).indgen!

   total = 0
   max_loop.times { total += a.mean }
   p Time.now - time
   p total, total/max_loop

   harp:~ > ruby a.rb
   0.266714
   9999990.0
   499999.5

and this is only a

   harp:~ > grep -i mhz /proc/cpuinfo
   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:

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

···

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

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 => AIM:petdance

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:

   harp:~ > cat a.rb
   require 'narray'

   time = Time.now

   max_loop = (ARGV.shift || 20).to_i
   max_size = (ARGV.shift || 1_000_000).to_i
   a = NArray.float(max_size).indgen + 1

   total = 0
   max_loop.times { total += a.mean }
   p Time.now - time
   p total, total/max_loop

   harp:~ > ruby a.rb
   0.283084
   10000010.0
   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

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

   harp:~ > cat a.lua
   function average( list )
     local sum = 0
     for i = 1, #list do
       sum = sum + list[i]
     end
     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
     total = total + average( a )
   end
   print( os.clock() - time )
   print( total )
   print( total/max_loop )

   harp:~ > lua a.lua
   2.44
   10000010
   500000.5

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

   harp:~ > cat a.rb
   require 'narray'

   time = Time.now

   max_loop = (ARGV.shift || 20).to_i
   max_size = (ARGV.shift || 1_000_000).to_i
   a = NArray.float(max_size).indgen + 1

   total = 0
   max_loop.times { total += a.mean }
   p Time.now - time
   p total, total/max_loop

   harp:~ > ruby a.rb
   0.287334
   10000010.0
   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

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

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.

Not to beat a dead horse, but...

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:

gotcha. still, __ruby__ is a compiled C extension :wink:

-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

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 => AIM:petdance

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 :wink:

-a
--
we can deny everything, except that we have the possibility of being
better.
simply reflect on that.
- the dalai lama

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 => AIM:petdance