diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /vendor/countme/examples/bench.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/countme/examples/bench.rs')
-rw-r--r-- | vendor/countme/examples/bench.rs | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/vendor/countme/examples/bench.rs b/vendor/countme/examples/bench.rs new file mode 100644 index 000000000..1f9cc967f --- /dev/null +++ b/vendor/countme/examples/bench.rs @@ -0,0 +1,69 @@ +use std::thread; + +use countme::{get, Count}; + +#[derive(Default)] +struct Foo { + _c: Count<Self>, +} + +#[derive(Default)] +struct Bar { + _c: Count<Self>, + _x: i32, +} + +mod deeply { + pub(crate) mod nested { + pub(crate) mod module { + use countme::Count; + + #[derive(Default)] + pub(crate) struct Quux { + _c: Count<Self>, + } + } + } +} + +fn main() { + countme::enable(true); + let t = std::time::Instant::now(); + let n = 5; + let m = 1_000_000; + + let mut threads = Vec::new(); + for _ in 0..n { + threads.push(thread::spawn(move || { + for _ in 0..m { + Foo::default(); + } + })); + threads.push(thread::spawn(move || { + let mut xs = Vec::with_capacity(m); + for _ in 0..m { + xs.push(Bar::default()) + } + })); + threads.push(thread::spawn(move || { + for _ in 0..m { + deeply::nested::module::Quux::default(); + } + })); + } + for t in threads { + t.join().unwrap(); + } + + let foo = get::<Foo>(); + assert_eq!(foo.total, m * n); + assert!(foo.max_live >= 1); + assert_eq!(foo.live, 0); + + let bar = get::<Bar>(); + assert_eq!(bar.total, m * n); + assert!(bar.max_live >= m); + assert_eq!(bar.live, 0); + + println!("{:?}", t.elapsed()); +} |