From 960de37e9bf62c8a9a63e81edc20b77587698a3a Mon Sep 17 00:00:00 2001 From: Wendell Smith Date: Sun, 8 Mar 2020 18:14:00 -0400 Subject: [PATCH] Use Sieve in documentation, its faster --- benches/benches.rs | 26 ++++++++++++-------------- src/lib.rs | 16 ++++++++-------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/benches/benches.rs b/benches/benches.rs index 1478233..c736e00 100644 --- a/benches/benches.rs +++ b/benches/benches.rs @@ -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)) diff --git a/src/lib.rs b/src/lib.rs index 311b9dd..8e6daf9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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); @@ -24,9 +24,9 @@ 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); @@ -34,9 +34,9 @@ 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); } @@ -44,9 +44,9 @@ for (ix, n) in pset.iter().enumerate().skip(1_000).take(10) { # 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);