Skip to content

Commit

Permalink
Parallelisation (#20)
Browse files Browse the repository at this point in the history
Process `Vec`s with more than 300k elements in parallel
  • Loading branch information
MusicalNinjaDad authored May 12, 2024
1 parent 28b8d8f commit c8388dc
Show file tree
Hide file tree
Showing 10 changed files with 723 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# FizzBuzz Changelog

## Rust 2.1.0 & Python 1.3.0

- Process `Vec`s / `list`s with more than 300k elements in parallel

## Rust 2.0.0 & Python 1.2.0

### Rust
Expand Down
2 changes: 1 addition & 1 deletion __pyversion__
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.0
1.3.0
5 changes: 4 additions & 1 deletion fizzbuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
[package]
name = "fizzbuzz"
version = "2.0.0"
version = "2.1.0"
edition = "2021"

[lib]
name = "fizzbuzz"
path = "src/lib.rs"
crate-type = ["rlib"] # cdylib required for python import, rlib required for rust tests.

[dependencies]
rayon = "1.10.0"

[dev-dependencies]
googletest = "0.11.0"
criterion = { version = "0.5", features = ["html_reports"] }
Expand Down
66 changes: 56 additions & 10 deletions fizzbuzz/benches/bench_fizzbuzz.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,71 @@
#![allow(dead_code)]
use criterion::{criterion_group, criterion_main, Criterion};
use fizzbuzz::{self, FizzBuzz, MultiFizzBuzz};
use rayon::prelude::*;

static TEST_SIZE: isize = 1_000_000;

#[inline]
fn fb100_000() {
for _ in 1..10 {
for i in 1..100_000 {
i.fizzbuzz();
}
fn for_loop() {
for i in 1..TEST_SIZE {
let _: String = i.fizzbuzz().into();
}
}

#[inline]
fn fb100_000_vec() {
for _ in 1..10 {
(1..100_000).collect::<Vec<_>>().fizzbuzz();
fn for_loop_with_vec_overhead() {
let inputs: Vec<_> = (1..TEST_SIZE).collect();
let mut out: Vec<String> = vec![];
for i in inputs.into_iter() {
out.push(i.fizzbuzz().into());
}
}

#[inline]
fn vec_iter() {
let inputs: Vec<_> = (1..TEST_SIZE).collect();
let _: Vec<String> = inputs.iter().map(|num| num.fizzbuzz().into()).collect();
}

#[inline]
fn vec_intoiter() {
let inputs: Vec<_> = (1..TEST_SIZE).collect();
let _: Vec<String> = inputs
.into_iter()
.map(|num| num.fizzbuzz().into())
.collect();
}

#[inline]
fn vec_pariter() {
let inputs: Vec<_> = (1..TEST_SIZE).collect();
let _: Vec<String> = inputs.par_iter().map(|num| num.fizzbuzz().into()).collect();
}

#[inline]
fn multifizzbuzz_trait() {
let inputs: Vec<_> = (1..TEST_SIZE).collect();
let _: Vec<String> = inputs.fizzbuzz().into();
}

#[inline]
fn multifizzbuzz_trait_as_string() {
let inputs: Vec<_> = (1..TEST_SIZE).collect();
let _: String = inputs.fizzbuzz().into();
}

fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("fizzbuzz 100_000", |b| b.iter(|| fb100_000()));
c.bench_function("fizzbuzz 100_000_vector", |b| b.iter(|| fb100_000_vec()));
// c.bench_function("for_loop", |b| b.iter(|| for_loop()));
// c.bench_function("for_loop_with_vec_overhead", |b| {
// b.iter(|| for_loop_with_vec_overhead())
// });
c.bench_function("vec_iter", |b| b.iter(|| vec_iter()));
// c.bench_function("vec_intoiter", |b| b.iter(|| vec_intoiter()));
c.bench_function("vec_pariter", |b| b.iter(|| vec_pariter()));
c.bench_function("multifizzbuzz_trait", |b| b.iter(|| multifizzbuzz_trait()));
c.bench_function("multifizzbuzz_trait_as_string", |b| {
b.iter(|| multifizzbuzz_trait_as_string())
});
}

criterion_group!(benches, criterion_benchmark);
Expand Down
Loading

0 comments on commit c8388dc

Please sign in to comment.