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

On 1/31/07, Miroslaw M. [email protected] 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

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

ruby-prof:
http://on-ruby.blogspot.com/2006/08/profile-and-ruby-prof.html
http://on-ruby.blogspot.com/2006/08/profile-and-ruby-prof-getting-specific.html
http://on-ruby.blogspot.com/2006/08/ruby-prof-and-call-graphs.html

RubyInline:
http://on-ruby.blogspot.com/2006/07/rubyinline-making-making-things-faster.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, Jan S. [email protected] wrote:

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

http://on-ruby.blogspot.com/2006/07/rubyinline-going-bit-further.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/09/recursive_functions_in_rubyinline.html

On Thu, 1 Feb 2007, William J. wrote:

Float(self.inject {|sum, elem| sum += elem }) /

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

On Jan 31, 8:31 am, “pat eyler” [email protected] wrote:

On 1/31/07, Jan S. [email protected] 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:

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

– 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

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

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

Jason

On Thu, 1 Feb 2007, Jason R. 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 Jan 31, 2007, at 3:57 PM, Jason R. wrote:

For performance, use a compiled language?

I like that advice MUCH better.

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

Almost, but not quite, is…

http://wiki.rubygarden.org/Ruby/page/show/RubyOptimization

On Wed, 31 Jan 2007, Miroslaw M. 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 C. Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : [email protected]
New Zealand

Jan S. 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?

On Thu, 1 Feb 2007, Jason R. 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

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

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.

On Feb 1, 2007, at 9:24 AM, Paul B. wrote:

On Thu, Feb 01, 2007 at 08:50:06AM +0900, Andy L. 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.

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

On Thu, Feb 01, 2007 at 08:50:06AM +0900, Andy L. 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.

Paul

On 2/1/07, Andy L. [email protected] wrote:

Wow, so angry.


Andy L. => [email protected] => www.petdance.com => AIM:petdance

Which is a pitty because it is very reasonable to give a second thought
to
“compiled language”
also I think that it is completely clear what you mean Jason it is
completely clear to me what Andy means.

Compiled to what? Now I am pushing it, but parsing into an AST and than
interpreting it is not considered as compilation, right now. And if
tomorrow the AST interpreter includes JIT?

Cheers
Robert

On Fri, 2 Feb 2007, William J. wrote:

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 J. wrote:

And this is slower than pure interpreted Lua (on a 3.19GHz machine). Try
it.

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 Jan 31, 3:17 pm, [email protected] wrote:

def ravg
a = (1…max_size).to_a
10000010.0
time = Time.now
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

( a = NArray.float max_size ).indgen!

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs