[#14464] who uses Python or Ruby, and for what? — ellard2@...01.fas.harvard.edu (-11,3-3562,3-3076)

A while ago I posted a request for people to share their experiences

12 messages 2001/05/01

[#14555] Ruby as a Mac OS/X scripting language — Dave Thomas <Dave@...>

10 messages 2001/05/02

[#14557] Arggg Bitten by the block var scope feature!!! — Wayne Scott <wscott@...>

13 messages 2001/05/02

[#14598] Re: Arggg Bitten by the block var scope feature!!! — "Conrad Schneiker" <schneik@...>

# On Thu, 3 May 2001, Wayne Scott wrote:

9 messages 2001/05/03

[#14636] Yet another "About private methods" question — Eric Jacoboni <jacoboni@...2.fr>

I'm still trying to figure out the semantics of private methods in Ruby.

39 messages 2001/05/04
[#14656] Re: Yet another "About private methods" question — Dave Thomas <Dave@...> 2001/05/04

Eric Jacoboni <jaco@teaser.fr> writes:

[#14666] Ruby and Web Applications — "Chris Montgomery" <monty@...> 2001/05/04

Greetings from a newbie,

[#14772] Re: Ruby and Web Applications — Jim Freeze <jim@...> 2001/05/07

On Sat, 5 May 2001, Chris Montgomery wrote:

[#14710] Why's Ruby so slow in this case? — Stefan Matthias Aust <sma@3plus4.de>

Sure, Ruby, being interpreted, is slower than a compiled language.

12 messages 2001/05/05

[#14881] Class/Module Information — "John Kaurin" <jkaurin@...>

It is possible to modify the following code to produce

18 messages 2001/05/09

[#15034] Re: calling .inspect on array/hash causes core dump — ts <decoux@...>

>>>>> "A" == Andreas Riedl <viisi@chello.at> writes:

15 messages 2001/05/12

[#15198] Re: Q: GUI framework with direct drawing ca pabilities? — Steve Tuckner <SAT@...>

Would it be a good idea to develop a pure Ruby GUI framework built on top of

13 messages 2001/05/15

[#15234] Pluggable sorting - How would you do it? — "Hal E. Fulton" <hal9000@...>

Hello all,

16 messages 2001/05/16

[#15549] ColdFusion for Ruby — "Michael Dinowitz" <mdinowit@...2000.com>

I don't currently use Ruby. To tell the truth, I have no real reason to. I'd

12 messages 2001/05/22

[#15569] I like ruby-chan ... — Rob Armstrong <rob@...>

Ruby is more human(e) than Python. We already have too many animals :-).

15 messages 2001/05/23

[#15601] How to avoid spelling mistakes of variable names — ndrochak@... (Nick Drochak)

Since Ruby does not require a variable to be declared, do people find

13 messages 2001/05/23

[#15734] java based interpreter and regexes — "Wayne Blair" <wayne.blair@...>

I have been thinking about the java based ruby interpreter project, and I

48 messages 2001/05/25

[#15804] is it possible to dynamically coerce objects types in Ruby? — mirian@... (Mirian Crzig Lennox)

Greetings to all. I am a newcomer to Ruby and I am exploring the

13 messages 2001/05/27
[#15807] Re: is it possible to dynamically coerce objects types in Ruby? — matz@... (Yukihiro Matsumoto) 2001/05/27

Hi,

[#15863] Experimental "in" operator for collections — Stefan Matthias Aust <sma@3plus4.de>

There's one thing where I prefer Python over Ruby. Testing whether an

13 messages 2001/05/28

[#15925] Re: Block arguments vs method arguments — ts <decoux@...>

>>>>> "M" == Mike <mike@lepton.fr> writes:

43 messages 2001/05/29
[#16070] Re: Block arguments vs method arguments — "Hal E. Fulton" <hal9000@...> 2001/05/31

----- Original Message -----

[#16081] Re: Block arguments vs method arguments — Sean Russell <ser@...> 2001/05/31

On Thu, May 31, 2001 at 11:53:17AM +0900, Hal E. Fulton wrote:

[#16088] Re: Block arguments vs method arguments — Dan Moniz <dnm@...> 2001/05/31

At 11:01 PM 5/31/2001 +0900, Sean Russell wrote:

[#15954] new keyword idea: tryreturn, tryturn or done — Juha Pohjalainen <voidjump@...>

Hello everyone!

12 messages 2001/05/29

[ruby-talk:14747] Re: Why's Ruby so slow in this case?

From: Aleksi Niemela <aleksi@...>
Date: 2001-05-06 17:44:20 UTC
List: ruby-talk #14747
In [ruby-talk:14710] titled "Why's Ruby so slow in this case?"
Stefan Matthias Aust wrote:

> However, the following code needs more than 15 minutes to complete
> while the original C source runs in less than one second!

He also continues:

> I wonder whether there are some ways to optimize this code other than
> to completely change the algorithm.  Or can the Ruby interpreter be
> improved?  I'm using Ruby 1.6.4 if that matters.

The Ruby interpreter isn't as fast as it probably could be. I'm not
familiar with the details why and how much the interpreter itself is
slower than certain others. Neither do I know what limits the Ruby's
way for method searching imposes and how does that compare to others,
like Smalltalk.

So I'm afraid I have to say that there isn't any magic bullet to cure
all your speed problems. But there are couple of basic things you 
could try to do for optimizating your code, like:

1) reduce calling, as the searching the methods and restoring contexts
   take some time 

      def odd(x)
        x % 2 == 1
      end
      p odd(6)

   =>

      p (6%2 == 1)

2) move your hand-made logic to be done inside built-in methods

      100_000.times do
        a = (0..100).to_a
        b = []
        for i in 0..100
          b << a[i] if i%2 == 0
        end
      end

   =>

      100_000.times do
        a = (0..100).to_a
        b = a.collect do |i|
          i%2 == 0
        end
      end

3) instantiate as few objects as possible, as the garbage collecting
   takes considerable amounts of time


But as you indicated the best way around performance problems is
usually to rewrite the entire logic (replace the algorithm). When the
original took over minute to produce the 35x35 map, this version works
quite a bit faster and builds a 80x80 map under half a second on my
machine.

Here's a version I wrote. It's also a bit "rubified" compared to original.
No assurances of it's alikeness, as I didn't quite understand the
position logic for hexas.

    - Aleksi



class Hex
  attr_accessor :terrain
  def initialize(terrain = Terrain::Water)
    @terrain = terrain
  end
  def iswater?
    @terrain == Terrain::Water
  end
  def ==(hex)
    @terrain == hex.terrain
  end
end

module Terrain
  (Water, Plain, Forest, Mountain, ) = (0..100).to_a
  Terrains = [Water, Plain, Forest, Mountain]
end

class Map
  attr_accessor :map, :size, :shores

  def initialize(size)
    @size = size
    @map = (0...size*size).map{Hex.new}
    @shores = {}
    # add the initial terrain
    markTerrain(rand(@size), rand(@size), Terrain::Terrains[rand(3)+1])
  end

  def makeLand
    shores = @shores.keys
    h = shores[rand(shores.size)]
    markTerrain(h%@size, h/@size, Terrain::Terrains[rand(3)+1])
  end
  
  XD =   [ 0, +1, +1,  0, -1, -1]
  YD = [ [-1, -1,  0, +1,  0, -1],
         [-1,  0, +1, +1, +1,  0] ]
  def markTerrain(x, y, terrain)
    h = y*@size + x
    @map[h].terrain = terrain

    # this hex is certainly not an offshore anymore
    @shores.delete h

    # find new offshores
    yd = YD[x%2]
    6.times do |i|
      x1 = x + XD[i]
      y1 = y + yd[i]
      next unless x1 >= 0 && x1 < @size && y1 >= 0 && y1 < @size
      h = y1*@size + x1
      @shores[h] = 1 if @map[h].iswater?
    end
  end

  def to_s
    s = ""
    terrainChar = ['~', 'p', 'f', 'm']
    for y in (0...@size)
      for x in (0...@size)
	s.concat terrainChar[@map[y*@size + x].terrain]
      end
      s.concat "\n"
    end
    s
  end
  
end

def createMap
  size = 80
  map = Map.new(size)
  (size*size/2-1).times do map.makeLand end
  print map
end

createMap

In This Thread

Prev Next