Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 61 lines (49 sloc) 1.273 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
require "rubygems"
require "rake"
require "benchmark"

require "rake/extensiontask"
Rake::ExtensionTask.new("sieve") do |extension|
  extension.lib_dir = "lib/sieve"
end

task :build => [:clean, :compile]

require "cucumber/rake/task"
Cucumber::Rake::Task.new(:cucumber => :build)

task :default => :cucumber

desc "Benchmark C implementation against pure Ruby implementation of the Sieve"
task(:benchmark => :build) do
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "lib"))
  require "sieve"

  def sieve(n)
    numbers = (0..n).to_a
    numbers[0] = numbers[1] = nil
    numbers.each do |num|
      next unless num
      break if num**2 > n
      (num**2).step(n, num) {|idx| numbers[idx] = nil }
    end
    numbers.compact
  end

  Benchmark.bmbm(15) do |benchmark|
    range = (0..1000000)
    step = 100000

    benchmark.report("sieve method") do
      range.step(step) do |i|
        sieve(i)
      end
    end

    benchmark.report("Numeric#sieve") do
      range.step(step) do |i|
        i.sieve
      end
    end

    benchmark.report("sieve 10_000_000") do
      sieve 10_000_000
    end

    benchmark.report("10_000_000.sieve") do
      10_000_000.sieve
    end

    benchmark.report("100_000_000.sieve") do
      100_000_000.sieve
    end
  end
end
Something went wrong with that request. Please try again.