
class Time
  # Returns the time taken in the block, in seconds
  def self.time
    if block_given?
      bef = Time.new
      yield
      aft = Time.new
      aft-bef      
    end
  end
end

str = "banananananana"
regexp1 = /ana/
regexp2 = /bana/
sum = 0

time1 = Time.time { 
  1.upto(100000) { |ix|
    if ix % 2 == 0
      if str =~ /ana/
        sum += 1
      end
    else
      if str =~ /bana/
        sum += 2
      end
    end
  }
}

sum2 = 0

time2 = Time.time { 
  1.upto(100000) { |ix|
    if ix % 2 == 0
      if str =~ regexp1
        sum2 += 1
      end
    else
      if str =~ regexp2
        sum2 += 2
      end
    end
  }
}


puts "Time1 #{time1} for sum #{sum}"
puts "Time2 #{time2} for sum #{sum2}"
