mirror of
https://github.com/uuid-rs/uuid.git
synced 2025-09-26 20:41:01 +00:00
add an example of generating bulk v7 UUIDs
This commit is contained in:
parent
4a129e7281
commit
864239ba30
@ -2,10 +2,44 @@
|
||||
#![feature(test)]
|
||||
extern crate test;
|
||||
|
||||
use std::time::SystemTime;
|
||||
|
||||
use test::Bencher;
|
||||
use uuid::Uuid;
|
||||
use uuid::{ContextV7, NoContext, Timestamp, Uuid};
|
||||
|
||||
#[bench]
|
||||
fn now_v7(b: &mut Bencher) {
|
||||
b.iter(|| Uuid::now_v7());
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn new_v7_no_context(b: &mut Bencher) {
|
||||
b.iter(|| Uuid::new_v7(Timestamp::now(NoContext)));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn new_v7_context(b: &mut Bencher) {
|
||||
let ctxt = ContextV7::new();
|
||||
|
||||
b.iter(|| Uuid::new_v7(Timestamp::now(&ctxt)));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn v7_raw(b: &mut Bencher) {
|
||||
let now = SystemTime::UNIX_EPOCH.elapsed().unwrap();
|
||||
let secs = now.as_secs();
|
||||
let subsec_nanos = now.subsec_nanos();
|
||||
let mut counter = 0;
|
||||
|
||||
b.iter(|| {
|
||||
Uuid::new_v7(Timestamp::from_unix_time(
|
||||
secs,
|
||||
subsec_nanos,
|
||||
{
|
||||
counter += 1;
|
||||
counter
|
||||
},
|
||||
42,
|
||||
))
|
||||
});
|
||||
}
|
||||
|
@ -12,6 +12,10 @@ path = "src/random_uuid.rs"
|
||||
name = "sortable_uuid"
|
||||
path = "src/sortable_uuid.rs"
|
||||
|
||||
[[example]]
|
||||
name = "sortable_uuid_bulk"
|
||||
path = "src/sortable_uuid_bulk.rs"
|
||||
|
||||
[[example]]
|
||||
name = "uuid_macro"
|
||||
path = "src/uuid_macro.rs"
|
||||
|
15
examples/src/sortable_uuid_bulk.rs
Normal file
15
examples/src/sortable_uuid_bulk.rs
Normal file
@ -0,0 +1,15 @@
|
||||
//! Generating a sortable UUID.
|
||||
//!
|
||||
//! If you enable the `v7` feature you can generate sortable UUIDs.
|
||||
//! This example avoids the synchronization cost of `Uuid::now_v7()`
|
||||
//! when generating UUIDs in bulk.
|
||||
|
||||
fn main() {
|
||||
use uuid::{ContextV7, Timestamp, Uuid};
|
||||
|
||||
let ctxt = ContextV7::new();
|
||||
|
||||
for _ in 0..10 {
|
||||
println!("{}", Uuid::new_v7(Timestamp::now(&ctxt)));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user