Browse files

Clean up benchmark code

  • Loading branch information...
1 parent 62a47fe commit 8d53ac15bce873db1038cd7cf6cca97bb0d73e27 @joshuaclayton committed Aug 28, 2010
Showing with 51 additions and 39 deletions.
  1. +18 −6 README.markdown
  2. +33 −0 Rakefile
  3. +0 −32 benchmark.rb
  4. +0 −1 sieve.gemspec
View
24 README.markdown
@@ -37,17 +37,29 @@ As far as I know, this is the most optimized pure Ruby sieve written.
The benchmarks were run on a 2.8GHz Intel Core 2 Duo MacBook Pro with 8 GB memory.
-Ruby 1.8.7p253 REE 2010.02
+ruby 1.8.6 (2010-02-05 patchlevel 399) [i686-darwin10.4.0]
user system total real
- sieve method 32.820000 0.710000 33.530000 ( 33.727176)
- Numeric#sieve 1.040000 0.420000 1.460000 ( 1.476743)
+ sieve method 41.430000 0.600000 42.030000 ( 42.261872)
+ Numeric#sieve 1.010000 0.250000 1.260000 ( 1.342326)
-Ruby 1.9.2p0
+ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-darwin10.4.0]
user system total real
- sieve method 21.460000 0.620000 22.080000 ( 22.170883)
- Numeric#sieve 0.940000 0.320000 1.260000 ( 1.262891)
+ sieve method 72.230000 0.970000 73.200000 ( 73.462660)
+ Numeric#sieve 0.970000 0.360000 1.330000 ( 1.334183)
+
+ruby 1.8.7 (2010-04-19 patchlevel 253) [i686-darwin10.4.0], MBARI 0x6770, Ruby Enterprise Edition 2010.02
+
+ user system total real
+ sieve method 86.790000 0.940000 87.730000 ( 87.804085)
+ Numeric#sieve 1.010000 0.380000 1.390000 ( 1.393168)
+
+ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-darwin10.4.0]
+
+ user system total real
+ sieve method 21.430000 0.480000 21.910000 ( 21.900469)
+ Numeric#sieve 0.890000 0.270000 1.160000 ( 1.146750)
## Author
View
33 Rakefile
@@ -1,5 +1,7 @@
require "rubygems"
require "rake"
+require "benchmark"
+require "sieve"
require "rake/extensiontask"
Rake::ExtensionTask.new("sieve") do |extension|
@@ -12,3 +14,34 @@ Cucumber::Rake::Task.new(:cucumber => [:clean, :compile]) do |t|
end
task :default => :cucumber
+
+desc "Benchmark C implementation against pure Ruby implementation of the Sieve"
+task(:benchmark => [:clean, :compile]) do
+ def sieve(n)
+ numbers = (0..n).map {|i| i }
+ 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.bm(15) do |benchmark|
+ range = (0..1000000)
+ step = 10000
+
+ 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
+ end
+end
View
32 benchmark.rb
@@ -1,32 +0,0 @@
-#!/usr/bin/env ruby
-
-require "rubygems"
-require "benchmark"
-require "sieve"
-
-def sieve(n)
- numbers = (0..n).map(&:to_i)
- 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.bm(15) do |benchmark|
- range = (0..1000000)
- step = 10000
- benchmark.report("sieve method") do
- range.step(step).each do |i|
- sieve(i)
- end
- end
-
- benchmark.report("Numeric#sieve") do
- range.step(step).each do |i|
- i.sieve
- end
- end
-end
View
1 sieve.gemspec
@@ -15,7 +15,6 @@ Gem::Specification.new do |s|
"MIT-LICENSE",
"README.markdown",
"Rakefile",
- "benchmark.rb",
"ext/sieve/extconf.rb",
"ext/sieve/sieve.c",
"ext/sieve/sieve.h",

0 comments on commit 8d53ac1

Please sign in to comment.