Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 62 lines (49 sloc) 1.273 kb
2c13ae0 @joshuaclayton Initial
authored
1 require "rubygems"
2 require "rake"
8d53ac1 @joshuaclayton Clean up benchmark code
authored
3 require "benchmark"
9004f12 @joshuaclayton Fix load path to use correct copy of sieve when benchmarking
authored
4
2c13ae0 @joshuaclayton Initial
authored
5 require "rake/extensiontask"
6 Rake::ExtensionTask.new("sieve") do |extension|
7 extension.lib_dir = "lib/sieve"
8 end
9
8e9899b @joshuaclayton Clean and compile as their own rake task
authored
10 task :build => [:clean, :compile]
11
2c13ae0 @joshuaclayton Initial
authored
12 require "cucumber/rake/task"
8e49013 @joshuaclayton Update dependencies and remove RCov
authored
13 Cucumber::Rake::Task.new(:cucumber => :build)
2c13ae0 @joshuaclayton Initial
authored
14
15 task :default => :cucumber
8d53ac1 @joshuaclayton Clean up benchmark code
authored
16
17 desc "Benchmark C implementation against pure Ruby implementation of the Sieve"
8e9899b @joshuaclayton Clean and compile as their own rake task
authored
18 task(:benchmark => :build) do
195cb5b @joshuaclayton Rakefile loads sieve in the correct spot
authored
19 $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "lib"))
20 require "sieve"
21
8d53ac1 @joshuaclayton Clean up benchmark code
authored
22 def sieve(n)
8f2686f @joshuaclayton Update Ruby sieve implementation for benchmark to be faster
authored
23 numbers = (0..n).to_a
8d53ac1 @joshuaclayton Clean up benchmark code
authored
24 numbers[0] = numbers[1] = nil
25 numbers.each do |num|
26 next unless num
27 break if num**2 > n
28 (num**2).step(n, num) {|idx| numbers[idx] = nil }
29 end
30 numbers.compact
31 end
32
8beeefc @joshuaclayton Get benchmarks correct
authored
33 Benchmark.bmbm(15) do |benchmark|
8d53ac1 @joshuaclayton Clean up benchmark code
authored
34 range = (0..1000000)
e80bf6b @joshuaclayton Update readme and benchmarks
authored
35 step = 100000
8d53ac1 @joshuaclayton Clean up benchmark code
authored
36
37 benchmark.report("sieve method") do
38 range.step(step) do |i|
39 sieve(i)
40 end
41 end
42
43 benchmark.report("Numeric#sieve") do
44 range.step(step) do |i|
45 i.sieve
46 end
47 end
d340b0d @joshuaclayton Add benchmark for running sieve against 10 million
authored
48
49 benchmark.report("sieve 10_000_000") do
50 sieve 10_000_000
51 end
52
53 benchmark.report("10_000_000.sieve") do
54 10_000_000.sieve
55 end
2cd51fe @joshuaclayton Benchmark sieve of 100 million
authored
56
57 benchmark.report("100_000_000.sieve") do
58 100_000_000.sieve
59 end
8d53ac1 @joshuaclayton Clean up benchmark code
authored
60 end
61 end
Something went wrong with that request. Please try again.