-
Notifications
You must be signed in to change notification settings - Fork 10
Add codspeed<>divan compat layer #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[package] | ||
name = "codspeed-divan-compat-examples" | ||
version = "0.0.0" | ||
edition = "2021" | ||
description = "Examples for Divan, a comfy benchmarking framework." | ||
publish = false | ||
license = "MIT OR Apache-2.0" | ||
|
||
[dependencies] | ||
fastrand = "2.3.0" | ||
divan = { package = "codspeed-divan-compat", path = ".." } | ||
|
||
[[bench]] | ||
name = "math" | ||
harness = false | ||
|
||
[[bench]] | ||
name = "sort" | ||
harness = false | ||
|
||
[[bench]] | ||
name = "time" | ||
harness = false |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Example benchmarks | ||
|
||
Mostly copied from [divan's examples](https://github.com/nvzqz/divan/tree/main/examples). | ||
GuillaumeLagrange marked this conversation as resolved.
Show resolved
Hide resolved
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
//! Copied from divan's exmaples | ||
use divan::black_box; | ||
use std::collections::{BTreeMap, HashMap}; | ||
|
||
fn main() { | ||
divan::main(); | ||
} | ||
|
||
#[divan::bench] | ||
fn add() -> i32 { | ||
black_box(2) + black_box(1) | ||
} | ||
|
||
#[divan::bench] | ||
#[ignore] | ||
fn sub() -> i32 { | ||
black_box(2) - black_box(1) | ||
} | ||
|
||
#[divan::bench] | ||
fn mul() -> i32 { | ||
black_box(2) * black_box(1) | ||
} | ||
|
||
#[divan::bench] | ||
fn div() -> i32 { | ||
black_box(2) / black_box(1) | ||
} | ||
|
||
#[divan::bench] | ||
fn rem() -> i32 { | ||
black_box(2) % black_box(1) | ||
} | ||
|
||
// 1, 1, 2, 3, 5, ... | ||
mod fibonacci { | ||
use super::*; | ||
|
||
const VALUES: &[u64] = &[0, 5, 10, 20, 30]; | ||
|
||
// O(n) | ||
#[divan::bench(args = VALUES)] | ||
fn iterative(n: u64) -> u64 { | ||
let mut previous = 1; | ||
let mut current = 1; | ||
|
||
for _ in 2..=n { | ||
let next = previous + current; | ||
previous = current; | ||
current = next; | ||
} | ||
|
||
current | ||
} | ||
|
||
// O(2^n) | ||
#[divan::bench(args = VALUES, max_time = 1)] | ||
fn recursive(n: u64) -> u64 { | ||
if n <= 1 { | ||
1 | ||
} else { | ||
recursive(n - 2) + recursive(n - 1) | ||
} | ||
} | ||
|
||
#[allow(dead_code)] | ||
trait Map: Default { | ||
fn get(&self, key: u64) -> Option<u64>; | ||
fn set(&mut self, key: u64, value: u64); | ||
} | ||
|
||
impl Map for HashMap<u64, u64> { | ||
fn get(&self, key: u64) -> Option<u64> { | ||
self.get(&key).copied() | ||
} | ||
|
||
fn set(&mut self, key: u64, value: u64) { | ||
self.insert(key, value); | ||
} | ||
} | ||
|
||
impl Map for BTreeMap<u64, u64> { | ||
fn get(&self, key: u64) -> Option<u64> { | ||
self.get(&key).copied() | ||
} | ||
|
||
fn set(&mut self, key: u64, value: u64) { | ||
self.insert(key, value); | ||
} | ||
} | ||
|
||
// Will be ignored in instrumented mode as we do not support type generics yet | ||
// O(n) | ||
#[cfg(not(codspeed))] | ||
#[divan::bench( | ||
types = [BTreeMap<u64, u64>, HashMap<u64, u64>], | ||
args = VALUES, | ||
)] | ||
fn recursive_memoized<M: Map>(n: u64) -> u64 { | ||
fn fibonacci<M: Map>(n: u64, cache: &mut M) -> u64 { | ||
if let Some(result) = cache.get(n) { | ||
return result; | ||
} | ||
|
||
if n <= 1 { | ||
return 1; | ||
} | ||
|
||
let result = fibonacci(n - 2, cache) + fibonacci(n - 1, cache); | ||
cache.set(n, result); | ||
result | ||
} | ||
|
||
fibonacci(n, &mut M::default()) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.