> I've heard complaints in the past that this library is slow.  One  
> reason is that
> it was implemented with continuations, which have performance  
> issues in the
> current version of Ruby.  "Was" is the keyword there though,  
> because I've just
> learned that Generator was recently re-implemented.  I learned some  
> good tricks
> reading the new version, so let's try fixing Generator ourselves.   
> (No peeking
> at the new version!)
>
> This week's Ruby Quiz is to write FasterGenerator, your own re- 
> implementation of
> Generator with an eye towards working faster.  (This is a small  
> library.  My
> version is 54 lines.)  It is possible to go even faster than the new
> implementation, with certain trade-offs:

With the same trade-offs as James' version(?) (i.e. no infinite block  
iterators) and with the hope that I don't have any bugs in the code,  
I get something like this on a G4 1.67Mhz (after changing the tests  
somewhat)

### next() ###

Rehearsal -----------------------------------------------------
Current Generator  20.360000  14.330000  34.690000 ( 51.788014)
My Generator        0.080000   0.010000   0.090000 (  0.116704)
------------------------------------------- total: 34.780000sec

                         user     system      total        real
Current Generator  23.200000  14.610000  37.810000 ( 53.145604)
My Generator        0.080000   0.010000   0.090000 (  0.129965)

(and this code)

8<----

tests = 10
enum  = (1..1000).to_a

puts
puts "### next() ###"
puts

Benchmark.bmbm do |x|
   x.report("Current Generator") do
     generator = Generator.new(enum)
     tests.times {
         generator.rewind
         generator.next until generator.end?
     }
   end
   x.report("My Generator") do
     generator = MyGenerator.new(enum)
     tests.times {
         generator.rewind
         generator.next until generator.end?
     }
   end
end

8<-----

I originally wanted to use the original values for tests and enum,  
but I got bored waiting.


/Christoffer