'. '

Ruby

From APIDesign

Revision as of 11:41, 2 October 2015 by JaroslavTulach (Talk | contribs)
(diff) ←Older revision | Current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search

Ruby is a programming language gaining some popularity in the first decade of 21st century due to lack of certain important features in Java. Some thing that these features invoke duck-typing, some like closures, but in fact the killer features is gems - standard packaging system for all Ruby libraries and frameworks.

Ruby was always known to be very slow language. Even when the hype around Rails was on - e.g. around 2006, there was nobody who could make the language fast. The great news is that OracleLabs decided to demonstrate the power of their Truffle framework on Ruby and are working on a fast Ruby implementation. OracleLabs claim that their implementation is ten times faster than the standard Ruby.

I wanted to verify such claim. One thing is being faster in a benchmark, the other thing is to speed up real programs. That is why I decided to write my first Ruby program - the Sieve of Eratosthenes and as following video demonstrates, Truffle based Ruby is really faster:

Is Truffle based Ruby ten times faster? The video shows that standard Ruby can compute about 5000 of prime numbers in 20s on my computer. The Truffle version was able to compute 17000 of primes. For a while I believed that this implies three times speed up. Not bad, but far less than the claim of being ten times faster.

However once, while showing the demo, I forgot to kill the standard Ruby version. After three minutes I noticed that and to my surprise the standard version was still fighting with 15000th prime number - e.g. it takes at least 180s to compute what Truffle Ruby can in 20s. The explanation is easy - computing first thousands of primes is way easier that finding the next thousands ones. The sieve of Eratosthenes algorithm isn't linear. Only comparing the time to compute the same amount of primes makes sense. However such time comparison actually shows ten times speed up!

class Natural
  def initialize
    @x = 2
  end
 
  def next
    return @x += 1
  end
end
natural = Natural.new
 
class Filter
  def initialize(number, filter)
    @number = number
    @filter = filter
  end
 
  def number
    @number
  end
 
  def filter
    @filter
  end
 
  def accept(n)
    filter = self
    loop do
      if (n % filter.number) == 0
        return false
      end
      filter = filter.filter;
      break if filter == nil
    end
    return true;
  end
end
 
class Primes
  def initialize(natural)
    @natural = natural
    @filter = nil;
  end
 
  def next
    loop do
      n = @natural.next
      if (@filter == nil || @filter.accept(n))
        @filter = Filter.new(n, @filter)
        return n;
      end
    end
  end
end
primes = Primes.new(natural)
 
puts "Press Ctrl-D to start..."
$stdin.read
 
start = Time.now
cnt = 0
res = ""
puts "Working..."
loop do
  p = primes.next
  res << "#{p}\n"
  cnt += 1
  if cnt % 1000 == 0
    puts res
    puts "Computed #{cnt} primes in #{Time.now - start} s"
    res = ""
  end
end


Truffle gives Ruby the speed it always needed.

Tim Boudreau on walking in circles

  • 1999 - Make everything an EJB!....2002: "The best way to win the J2EE game is not to play" (Bruce Tate - Better, Faster, Lighter Java)
  • 1980's - CASE tools generate giant wads of code for you; 1990's gee when there's a bug in my unreadable generated code, how do I fix it; 2006 or so...Ruby on Rails generates giant wads of code for you...
  • 1990's - RMI, other "object with one foot on each side of the wire" strategies - network-based objects; 2000's - gee, you have no idea what call sequence will block on network I/O - objects with one foot on each side of the wire are a horrible idea - let's do SOA instead (which is just procedural programming over sockets).

-- Tim Boudreau

Andreas Stefik's addition

Just chiming in here. I'll admit that when I read this I laughed heartily I couldn't agree more!

The ruby on rails folks are obsessive compulsive about how awesome and "easy" rails is (e.g., a blog in five minutes), I suspect because it generates giant wads of code for you, which they somehow assume never needs to modified, or possibly that modifying it is "easy" or --- I don't know, something. Whenever I use it, though, I think to myself, "crap, now how do I debug these giant wads of generated unreadable code." I swear, every time I touch rails, I spend 90% of my time twiddling around with silly configuration and generation issues (or updating to version 2.20001, which seemingly changed the way you interact with your generated code from 2.20000) and the rest on actually doing something useful. And keep in mind, I have a phd, write compilers in my spare time, and have done a "reasonable" amount of web programming for various projects, so this isn't exactly my first day.

Anyway, that's my two cents, for what it is worth (if anything).

-- Andreas Stefik

Does Ruby show signs of Good Technology? I guess its time to market is excellent (blog in five minutes). The coolness is also there (or at least used to be few years ago). As far as I know the total cost of ownership is not that low as one is used to. But it takes time to find out what TCO for some technology is. But maybe people are realizing that now. Which may also be the reason why ruby is not seen as cool as it used to be. At least that is my observation: the buzz has now switched elsewhere (for example to parallelism which is not ruby's strongest point either).

--JaroslavTulach 05:30, 28 January 2010 (UTC)

Personal tools
buy