Skip to content

Instantly share code, notes, and snippets.

@kenkoooo
Created December 11, 2022 03:47
Show Gist options
  • Save kenkoooo/09099d73f00b8a18b47c805d98e3f885 to your computer and use it in GitHub Desktop.
Save kenkoooo/09099d73f00b8a18b47c805d98e3f885 to your computer and use it in GitHub Desktop.
[package]
name = "xxx"
version = "0.1.0"
edition = "2021"
[profile.release]
opt-level = 3
debug = 2
use std::{collections::HashMap, rc::Rc, time::Instant};
#[derive(Clone)]
pub enum WithMap {
List(List<WithMap>),
Map(Map<WithMap, WithMap>),
}
#[derive(Clone)]
pub enum NoMap {
List(List<NoMap>),
}
#[derive(Clone)]
pub struct List<T>(pub Rc<Vec<T>>);
#[derive(Clone)]
pub struct Map<K, V>(pub HashMap<K, V>);
fn no_map() {
let start = Instant::now();
for _ in 0..2000 {
let obj = NoMap::List(List(Rc::new(vec![])));
for _ in 0..100000 {
let _ = obj.clone();
}
let list = List::<NoMap>(Rc::new(vec![]));
for _ in 0..100000 {
let _ = list.clone();
}
}
println!("no-map: {}ms", start.elapsed().as_millis());
}
fn with_map() {
let start = Instant::now();
for _ in 0..2000 {
let obj = WithMap::List(List(Rc::new(vec![])));
for _ in 0..100000 {
let _ = obj.clone();
}
let list = List::<WithMap>(Rc::new(vec![]));
for _ in 0..100000 {
let _ = list.clone();
}
}
println!("with-map: {}ms", start.elapsed().as_millis());
}
fn main() {
no_map();
with_map();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment