sieve 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +9 -2
- data/benchmark.rb +32 -0
- data/ext/sieve/sieve.c +12 -9
- data/ext/sieve/sieve.h +1 -1
- data/features/sieve.feature +5 -0
- data/features/step_definitions/sieve_steps.rb +10 -3
- metadata +5 -4
data/README.markdown
CHANGED
@@ -35,12 +35,19 @@ The sieve method itself looks like this:
|
|
35
35
|
|
36
36
|
As far as I know, this is the most optimized pure Ruby sieve written.
|
37
37
|
|
38
|
+
The benchmarks were run on a 2.8GHz Intel Core 2 Duo MacBook Pro with 8 GB memory.
|
39
|
+
|
40
|
+
Ruby 1.8.7p253 REE 2010.02
|
41
|
+
|
38
42
|
user system total real
|
39
43
|
sieve method 32.820000 0.710000 33.530000 ( 33.727176)
|
40
44
|
Numeric#sieve 1.040000 0.420000 1.460000 ( 1.476743)
|
41
45
|
|
42
|
-
|
43
|
-
|
46
|
+
Ruby 1.9.2p0
|
47
|
+
|
48
|
+
user system total real
|
49
|
+
sieve method 21.460000 0.620000 22.080000 ( 22.170883)
|
50
|
+
Numeric#sieve 0.940000 0.320000 1.260000 ( 1.262891)
|
44
51
|
|
45
52
|
## Author
|
46
53
|
|
data/benchmark.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "rubygems"
|
4
|
+
require "benchmark"
|
5
|
+
require "sieve"
|
6
|
+
|
7
|
+
def sieve(n)
|
8
|
+
numbers = (0..n).map(&:to_i)
|
9
|
+
numbers[0] = numbers[1] = nil
|
10
|
+
numbers.each do |num|
|
11
|
+
next unless num
|
12
|
+
break if num**2 > n
|
13
|
+
(num**2).step(n, num) {|idx| numbers[idx] = nil }
|
14
|
+
end
|
15
|
+
numbers.compact
|
16
|
+
end
|
17
|
+
|
18
|
+
Benchmark.bm(15) do |benchmark|
|
19
|
+
range = (0..1000000)
|
20
|
+
step = 10000
|
21
|
+
benchmark.report("sieve method") do
|
22
|
+
range.step(step).each do |i|
|
23
|
+
sieve(i)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
benchmark.report("Numeric#sieve") do
|
28
|
+
range.step(step).each do |i|
|
29
|
+
i.sieve
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/ext/sieve/sieve.c
CHANGED
@@ -3,15 +3,16 @@
|
|
3
3
|
#include "sieve.h"
|
4
4
|
|
5
5
|
void Init_sieve() {
|
6
|
-
VALUE rb_mSieve = rb_define_module("Sieve")
|
7
|
-
|
6
|
+
VALUE rb_mSieve = rb_define_module("Sieve"),
|
7
|
+
numeric = rb_define_class("Numeric", rb_cObject);
|
8
8
|
rb_define_method(rb_mSieve, "sieve", sieve, 0);
|
9
9
|
rb_include_module(numeric, rb_mSieve);
|
10
10
|
}
|
11
11
|
|
12
|
-
VALUE sieve(VALUE self) {
|
13
|
-
|
14
|
-
long
|
12
|
+
static VALUE sieve(const VALUE self) {
|
13
|
+
if(rb_num2long(self) < 0) { return Qnil; }
|
14
|
+
long number = rb_num2long(self) + 1,
|
15
|
+
* numbers = malloc(number * sizeof(long));
|
15
16
|
|
16
17
|
if(numbers == NULL) { return Qnil; }
|
17
18
|
|
@@ -21,13 +22,14 @@ VALUE sieve(VALUE self) {
|
|
21
22
|
}
|
22
23
|
numbers[0] = numbers[1] = -1;
|
23
24
|
|
25
|
+
long current_square;
|
24
26
|
for(i = 0; i < number; i++) {
|
25
|
-
|
26
|
-
if(numbers[i] == -1)
|
27
|
-
if(
|
27
|
+
current_square = powl(i, 2);
|
28
|
+
if(numbers[i] == -1) { continue; }
|
29
|
+
if(current_square > number) { break; }
|
28
30
|
|
29
31
|
long n;
|
30
|
-
for(n =
|
32
|
+
for(n = current_square; n < number; n += i) {
|
31
33
|
numbers[n] = -1;
|
32
34
|
}
|
33
35
|
}
|
@@ -40,5 +42,6 @@ VALUE sieve(VALUE self) {
|
|
40
42
|
}
|
41
43
|
|
42
44
|
free(numbers);
|
45
|
+
|
43
46
|
return primes_array;
|
44
47
|
}
|
data/ext/sieve/sieve.h
CHANGED
data/features/sieve.feature
CHANGED
@@ -1,10 +1,17 @@
|
|
1
|
-
When /^I run a sieve on (
|
1
|
+
When /^I run a sieve on (\-?\d+)$/ do |number|
|
2
2
|
@result = number.to_i.sieve
|
3
3
|
end
|
4
4
|
|
5
5
|
Then /^I should have the primes (.*)$/ do |primes|
|
6
|
-
|
7
|
-
|
6
|
+
case primes
|
7
|
+
when "nil"
|
8
|
+
@result.should be_nil
|
9
|
+
when ""
|
10
|
+
@result.should be_empty
|
11
|
+
else
|
12
|
+
@primes = primes.split(",").map(&:strip).map(&:to_i)
|
13
|
+
@result.should == @primes
|
14
|
+
end
|
8
15
|
end
|
9
16
|
|
10
17
|
When /^I load all the primes from "([^"]*)"$/ do |path|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sieve
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Josh Clayton
|
@@ -32,10 +32,11 @@ files:
|
|
32
32
|
- MIT-LICENSE
|
33
33
|
- README.markdown
|
34
34
|
- Rakefile
|
35
|
-
-
|
35
|
+
- benchmark.rb
|
36
36
|
- ext/sieve/extconf.rb
|
37
37
|
- ext/sieve/sieve.c
|
38
38
|
- ext/sieve/sieve.h
|
39
|
+
- lib/sieve.rb
|
39
40
|
- cucumber.yml
|
40
41
|
- features/sieve.feature
|
41
42
|
- features/step_definitions/sieve_steps.rb
|