Skip to content

Commit

Permalink
Use Sieve in documentation, its faster
Browse files Browse the repository at this point in the history
  • Loading branch information
wackywendell committed Mar 8, 2020
1 parent 6edb44c commit 960de37
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
26 changes: 12 additions & 14 deletions benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,20 @@ fn bench_primes(c: &mut Criterion) {
sizes.sort();
sizes.dedup();

let mut group = c.benchmark_group("TrialDivision::find");
let mut group = c.benchmark_group("find");
for &size in sizes.iter() {
group.throughput(Throughput::Elements(size));
group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, &size| {
b.iter(|| {
let mut pset = TrialDivision::new();
black_box(pset.find(size))
})
});
}
group.finish();

let mut group = c.benchmark_group("Sieve::find");
for &size in sizes.iter() {
group.throughput(Throughput::Elements(size));
group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, &size| {
group.bench_with_input(
BenchmarkId::new("TrialDivision", size),
&size,
|b, &size| {
b.iter(|| {
let mut pset = TrialDivision::new();
black_box(pset.find(size))
})
},
);
group.bench_with_input(BenchmarkId::new("Sieve", size), &size, |b, &size| {
b.iter(|| {
let mut pset = Sieve::new();
black_box(pset.find(size))
Expand Down
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ A basic library for finding primes, providing a basic Iterator over all primes.
The simplest usage is simply to create an `Iterator`:
```
use primes::{TrialDivision, PrimeSet};
use primes::{Sieve, PrimeSet};
let mut pset = TrialDivision::new();
let mut pset = Sieve::new();
for (ix, n) in pset.iter().enumerate().take(10) {
println!("Prime {}: {}", ix, n);
Expand All @@ -24,29 +24,29 @@ for the given test, and primes are cached for later use.
# Example: Find the first prime after 1 million
```
use primes::{TrialDivision, PrimeSet};
use primes::{Sieve, PrimeSet};
let mut pset = TrialDivision::new();
let mut pset = Sieve::new();
let (ix, n) = pset.find(1_000_000);
println!("Prime {}: {}", ix, n);
```
# Example: Find the first ten primes *after* the thousandth prime
```
use primes::{TrialDivision, PrimeSet};
use primes::{Sieve, PrimeSet};
let mut pset = TrialDivision::new();
let mut pset = Sieve::new();
for (ix, n) in pset.iter().enumerate().skip(1_000).take(10) {
println!("Prime {}: {}", ix, n);
}
```
# Example: Find the first prime greater than 1000
```
use primes::{TrialDivision, PrimeSet};
use primes::{Sieve, PrimeSet};
let mut pset = TrialDivision::new();
let mut pset = Sieve::new();
let (ix, n) = pset.find(1_000);
println!("The first prime after 1000 is the {}th prime: {}", ix, n);
Expand Down

0 comments on commit 960de37

Please sign in to comment.