Created
May 29, 2010 06:08
-
-
Save chastell/418076 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
require './integer' | |
require 'prime' | |
Benchmark.bmbm do |bench| | |
bench.report 'simple:' do | |
(2..100_000).each { |i| i.simple_prime? } | |
end | |
bench.report 'stdlib:' do | |
(2..100_000).each { |i| i.prime? } | |
end | |
bench.report 'inline:' do | |
(2..100_000).each { |i| i.inline_prime? } | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
require './integer' | |
require 'prime' | |
Benchmark.bmbm do |bench| | |
bench.report 'simple:' do | |
(2..100_000).each { |i| i.simple_prime? } | |
end | |
bench.report 'stdlib:' do | |
(2..100_000).each { |i| i.prime? } | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
require './integer' | |
require 'prime' | |
Benchmark.bmbm do |bench| | |
bench.report 'stupid:' do | |
(2..10_000).each { |i| i.stupid_prime? } | |
end | |
bench.report 'tedious:' do | |
(2..10_000).each { |i| i.tedious_prime? } | |
end | |
bench.report 'simple:' do | |
(2..10_000).each { |i| i.simple_prime? } | |
end | |
bench.report 'stdlib:' do | |
(2..10_000).each { |i| i.prime? } | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './integer' | |
require 'prime' | |
limit = ARGV.first.to_i | |
method = ARGV.last.to_sym | |
sum = (2...limit).select(&method).inject :+ | |
puts "sum(primes < #{limit}) = #{sum}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
gem install ruby_core_source ruby-prof perftools.rb RubyInline |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'inline' | |
class Integer | |
def stupid_prime? | |
prime = true | |
(2...self).each { |i| prime = false if (self % i).zero? } | |
prime | |
end | |
def tedious_prime? | |
(2...self).all? { |i| (self % i).nonzero? } | |
end | |
def simple_prime? | |
(2..Math.sqrt(self).floor).all? { |i| (self % i).nonzero? } | |
end | |
def inline_prime? | |
c_prime.nonzero? | |
end | |
inline do |builder| | |
builder.c ' | |
long c_prime() { | |
long number = NUM2LONG(self); | |
long i; | |
for (i = 2; i <= floor(sqrt(number)); i++) { | |
if (number % i == 0) return 0; | |
} | |
return 1; | |
} | |
' | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'prime' | |
puts Prime.take_while { |p| p < 2_000_000 }.inject :+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment