Ruby vs. Python and Euphoria: sieve benc hmark 
Author Message
 Ruby vs. Python and Euphoria: sieve benc hmark

Quote:
> > This is a MIME message in the multipart format

> Can you repost your question and attachment in plain-text format?

This is what I'd expect the python program to be. Say

  ruby -r base64 -e'puts decode64("IyBwc..his attachment..Q0KDQo=")'

        - Aleksi

# prime sieve in Python

# set 1 higher for Python for-loop:
BATCH = 51
BENCH_TIME = 15  # how many seconds to run benchmark
SIZE = 500
ON = 1
OFF = 0

from time import clock

def sieve():
        global flags
        count = 0
        flags = [ON]*(SIZE+1)

        for i in range(1, SIZE):
                still_prime = flags[i]
                if still_prime:
                        prime = 2 * i
                        prime = prime + 1
                        start = prime + i
                        for k in range(start, SIZE, prime):
                                flags[k] = OFF
                        count = count + 1
        return count

def main():
        global flags
        print "prime sieve benchmark ..."
        cycles = 0
        t = clock()
        while clock() < t + BENCH_TIME:
                for i in range(0, BATCH):
                        nprimes = sieve()
                        if nprimes != 167:
                                print "whoops!"
                cycles = cycles + BATCH

        t = clock() - t
        print "%g sieves per second" % (cycles / t)

        print "%3d" % 2,
        for i in range(1, SIZE):
                if flags[i]:
                        print "%3d" % (i*2+1),

main()



Sat, 12 Jul 2003 00:40:04 GMT  
 Ruby vs. Python and Euphoria: sieve benc hmark

Quote:
> Kenneth Rhodes writes:

> > Hi..... I am a Ruby novice, really just a curious inquirer.  I would
> > appreciate someone translating the attached python sieve benchmark
> > to RUBY, it would really help me get a handle on the way Ruby is
> > written.

Dave:

Quote:
> I didn't unpack your code, but you'll find python, Ruby, perl, and
> other Sieve benchmarks at:

>    http://www.bagley.org/~doug/shootout/bench/sieve/

> Click on the language to see the source.

I became curious a little and thought to translate the original source. I
wrapped it to a class, but didn't touch the original logic.

  class Sieve
    attr_accessor :size, :flags
    def initialize(size = 500)

    end

    def sieve
      count = 0



          prime = 2 * i
          prime = prime + 1
          start = prime + i


          end
          count += 1
        end
      end
      return count
    end
  end

  def benchmark(sieve, seconds = 15, batch = 50)
    puts "prime sieve benchmark"
    cycles = 0
    t = Time.now
    while Time.now < t + seconds
      batch.times do
        nprimes = sieve.sieve
        puts "whoops!" unless nprimes == 167
      end
      cycles += batch
    end

    t = Time.now - t
    puts "#{cycles / t} sieves per second"

    print "%3d " % 2
    1.upto(sieve.size) do |i|
      print "%3d " % (i*2 + 1) if sieve.flags[i]
    end
    puts
  end

  benchmark( Sieve.new(size = 500), seconds = 15, batch = 50)

The results:

sieve.py: 235.178 sieves per second      (15.11user)
sieve.rb: 225.616033 sieves per second   (15.02user)

Lefts me wonder why the Ruby test at Dougs site was dramatically faster than
Python, but in this version was slightly slower (except that this one used
less wall clock time :). I don't have time to investigate though.

        - Aleksi



Sat, 12 Jul 2003 01:54:53 GMT  
 Ruby vs. Python and Euphoria: sieve benc hmark

Quote:

> Lefts me wonder why the Ruby test at Dougs site was dramatically faster than
> Python, but in this version was slightly slower (except that this one used
> less wall clock time :). I don't have time to investigate though.

Because I wrote the one at Doug's site ;-)

Actually, the reasons are:

  1. I only create the array once, and then dup it for each iteration.

  2. The Python code seems to have a bug. It scans up from 2*i, rather
     than i*i.

That's be my guess, anyway.

Dave



Sat, 12 Jul 2003 02:24:25 GMT  
 Ruby vs. Python and Euphoria: sieve benc hmark
   Hi ..... thanks to Aleksi for porting the Python benchmark. However, I get a parse error referring to the line:

count +=1

when I try to run the  code below on my machine.

I've double checked the code.  Will try again later tonight.

I deeply appreciate the response to my inquiry.

Ken Rhodes

Quote:

> > Kenneth Rhodes writes:

> > > Hi..... I am a Ruby novice, really just a curious inquirer.  I would
> > > appreciate someone translating the attached python sieve benchmark
> > > to RUBY, it would really help me get a handle on the way Ruby is
> > > written.

> Dave:

> > I didn't unpack your code, but you'll find python, Ruby, perl, and
> > other Sieve benchmarks at:

> >    http://www.bagley.org/~doug/shootout/bench/sieve/

> > Click on the language to see the source.

> I became curious a little and thought to translate the original source. I
> wrapped it to a class, but didn't touch the original logic.

>   class Sieve
>     attr_accessor :size, :flags
>     def initialize(size = 500)

>     end

>     def sieve
>       count = 0



>           prime = 2 * i
>           prime = prime + 1
>           start = prime + i


>           end
>           count += 1
>         end
>       end
>       return count
>     end
>   end

>   def benchmark(sieve, seconds = 15, batch = 50)
>     puts "prime sieve benchmark"
>     cycles = 0
>     t = Time.now
>     while Time.now < t + seconds
>       batch.times do
>         nprimes = sieve.sieve
>         puts "whoops!" unless nprimes == 167
>       end
>       cycles += batch
>     end

>     t = Time.now - t
>     puts "#{cycles / t} sieves per second"

>     print "%3d " % 2
>     1.upto(sieve.size) do |i|
>       print "%3d " % (i*2 + 1) if sieve.flags[i]
>     end
>     puts
>   end

>   benchmark( Sieve.new(size = 500), seconds = 15, batch = 50)

> The results:

> sieve.py: 235.178 sieves per second      (15.11user)
> sieve.rb: 225.616033 sieves per second   (15.02user)

> Lefts me wonder why the Ruby test at Dougs site was dramatically faster than
> Python, but in this version was slightly slower (except that this one used
> less wall clock time :). I don't have time to investigate though.

>     - Aleksi

--
Ken Rhodes

__________________________________________________________________
Get your own FREE, personal Netscape Webmail account today at http://webmail.netscape.com/



Sat, 12 Jul 2003 06:19:48 GMT  
 Ruby vs. Python and Euphoria: sieve benc hmark

Quote:
>    Hi ..... thanks to Aleksi for porting the Python benchmark. However, I get a parse error referring to the line:

> count +=1

I believe that "+=" increment/assignment is supported in Python 2.0, but
not in 1.5.*.  You should be able to just change the line to

        count = count + 1

Quote:

> Ken Rhodes

David S.



Sat, 12 Jul 2003 06:52:17 GMT  
 Ruby vs. Python and Euphoria: sieve benc hmark
I am confused here...

It is a ruby version of the python benchmark I needed... the error reported below came from trying to run the code given from the Ruby interpreter on my machine.

Quote:

> >    Hi ..... thanks to Aleksi for porting the Python benchmark. However, I get a parse error referring to the line:

> > count +=1

> I believe that "+=" increment/assignment is supported in Python 2.0, but
> not in 1.5.*.  You should be able to just change the line to

>     count = count + 1

> > Ken Rhodes

> David S.


--
Ken Rhodes

__________________________________________________________________
Get your own FREE, personal Netscape Webmail account today at http://webmail.netscape.com/



Sat, 12 Jul 2003 13:43:07 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Ruby vs. Python and Euphoria: sieve benchmark

2. Python v. Euphoria

3. Final for World Series: Python vs Ruby

4. Ruby vs. Python: Decisions, Decisions

5. Ruby vs Python / Perl for Win32 Administration

6. Python vs. Ruby (and os.path.walk)

7. Python vs Ruby

8. Python vs Ruby

9. Python vs. Ruby: threads - advice appreciated

10. Python vs. Ruby

11. Python Binding [Was: Re: PYTHON VS. PERL VS. TCL ]

 

 
Powered by phpBB® Forum Software