Skip to content

Commit

Permalink
Merge pull request #19 from calcit-lang/loop-get
Browse files Browse the repository at this point in the history
Index with loop_get
  • Loading branch information
NoEgAm authored Feb 18, 2024
2 parents 3bf6b55 + 3ae75c5 commit 6462f88
Show file tree
Hide file tree
Showing 6 changed files with 362 additions and 297 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

[package]
name = "im_ternary_tree"
version = "0.0.14"
version = "0.0.17"
edition = "2021"
authors = ["jiyinyiyong <jiyinyiyong@gmail.com>"]
license = "MIT"
Expand All @@ -23,7 +23,7 @@ criterion = "0.3"


[[bench]]
name = "creating"
name = "accessing"
harness = false

[profile.release]
Expand Down
80 changes: 80 additions & 0 deletions benches/accessing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use criterion::{criterion_group, criterion_main, Criterion};

use im_ternary_tree::TernaryTreeList;

const ITER_SIZE: usize = 10000;

fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("index", |b| {
let mut data = TernaryTreeList::Empty;

for idx in 0..ITER_SIZE {
data = data.push(idx)
}

b.iter(|| {
for idx in 0..ITER_SIZE {
let _ = data[idx];
}
})
});

c.bench_function("get", |b| {
let mut data = TernaryTreeList::Empty;

for idx in 0..ITER_SIZE {
data = data.push(idx)
}

b.iter(|| {
for idx in 0..ITER_SIZE {
let _ = data.get(idx);
}
})
});

c.bench_function("loop_get", |b| {
let mut data = TernaryTreeList::Empty;

for idx in 0..ITER_SIZE {
data = data.push(idx)
}

b.iter(|| {
for idx in 0..ITER_SIZE {
let _ = data.loop_get(idx);
}
})
});

c.bench_function("ref_get", |b| {
let mut data = TernaryTreeList::Empty;

for idx in 0..ITER_SIZE {
data = data.push(idx)
}

b.iter(|| {
for idx in 0..ITER_SIZE {
let _ = data.ref_get(idx).to_owned();
}
})
});

c.bench_function("first", |b| {
let mut data = TernaryTreeList::Empty;

for idx in 0..ITER_SIZE {
data = data.push(idx)
}

b.iter(|| {
for _ in 0..ITER_SIZE {
let _ = data.first();
}
})
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
242 changes: 4 additions & 238 deletions benches/creating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ use criterion::{criterion_group, criterion_main, Criterion};

use im_ternary_tree::TernaryTreeList;

const ITER_SIZE: usize = 10000;

fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("creating list", |b| {
b.iter(|| {
let mut data = TernaryTreeList::Empty;

for idx in 0..1000 {
for idx in 0..ITER_SIZE {
data = data.push(idx)
}
})
Expand All @@ -17,247 +19,11 @@ fn criterion_benchmark(c: &mut Criterion) {
b.iter(|| {
let mut data = TernaryTreeList::Empty;

for idx in 0..1000 {
data = data.append(idx)
}
})
});

c.bench_function("append to list", |b| {
b.iter(|| {
let mut data = TernaryTreeList::Empty;

for idx in 0..1000 {
for idx in 0..ITER_SIZE {
data = data.append(idx)
}
})
});

c.bench_function("push_right list", |b| {
b.iter(|| {
let mut data = TernaryTreeList::Empty;

for idx in 0..1000 {
data = data.push_right(idx)
}
})
});

c.bench_function("unshift to list", |b| {
b.iter(|| {
let mut data = TernaryTreeList::Empty;

for idx in 0..1000 {
data = data.unshift(idx)
}
})
});

c.bench_function("push_left list", |b| {
b.iter(|| {
let mut data = TernaryTreeList::Empty;

for idx in 0..1000 {
data = data.push_left(idx)
}
})
});

c.bench_function("inserting in middle", |b| {
b.iter(|| {
let mut data = TernaryTreeList::Empty;

// TODO overflowed
for idx in 0..1000 {
let pos = idx / 2;
data = data.insert(pos, idx, false).unwrap()
}
})
});

c.bench_function("rest", |b| {
let mut data = TernaryTreeList::Empty;

for idx in 0..1000 {
data = data.push(idx);
}

b.iter(move || {
let mut d = data.to_owned();

while !d.is_empty() {
d = d.slice(1, d.len()).unwrap()
}
})
});

c.bench_function("rest from push_right", |b| {
let mut data = TernaryTreeList::Empty;

for idx in 0..1000 {
data = data.push_right(idx);
}

b.iter(move || {
let mut d = data.to_owned();

while !d.is_empty() {
d = d.slice(1, d.len()).unwrap()
}
})
});

c.bench_function("drop-left", |b| {
let mut data = TernaryTreeList::Empty;

for idx in 0..1000 {
data = data.push(idx);
}

b.iter(move || {
let mut d = data.to_owned();

while d.len() > 1 {
d = d.drop_left()
}
})
});

c.bench_function("drop-right", |b| {
let mut data = TernaryTreeList::Empty;

for idx in 0..1000 {
data = data.push(idx);
}

b.iter(move || {
let mut d = data.to_owned();

while d.len() > 1 {
d = d.drop_right()
}
})
});

c.bench_function("drop-left-shallow", |b| {
let mut data = TernaryTreeList::Empty;

for idx in 0..1000 {
data = data.push(idx);
}

b.iter(move || {
let mut d = data.to_owned();

while d.len() > 1 {
d = d.drop_left_shallow()
}
})
});

c.bench_function("drop-right-shallow", |b| {
let mut data = TernaryTreeList::Empty;

for idx in 0..1000 {
data = data.push(idx);
}

b.iter(move || {
let mut d = data.to_owned();

while d.len() > 1 {
d = d.drop_right_shallow()
}
})
});

c.bench_function("drop-left from push_right", |b| {
let mut data = TernaryTreeList::Empty;

for idx in 0..1000 {
data = data.push_right(idx);
}

b.iter(move || {
let mut d = data.to_owned();

while d.len() > 1 {
d = d.drop_left()
}
})
});

c.bench_function("slice", |b| {
let mut data = TernaryTreeList::Empty;

for idx in 0..1000 {
data = data.push(idx)
}

b.iter(move || {
let d = data.to_owned();

for _ in 0..1000 {
d.slice(300, 600).unwrap();
}
})
});

c.bench_function("index", |b| {
let mut data = TernaryTreeList::Empty;

for idx in 0..1000 {
data = data.push(idx)
}

b.iter(|| {
for idx in 0..1000 {
let _ = data[idx];
}
})
});

c.bench_function("get", |b| {
let mut data = TernaryTreeList::Empty;

for idx in 0..1000 {
data = data.push(idx)
}

b.iter(|| {
for idx in 0..1000 {
let _ = data.get(idx);
}
})
});

c.bench_function("loop_get", |b| {
let mut data = TernaryTreeList::Empty;

for idx in 0..1000 {
data = data.push(idx)
}

b.iter(|| {
for idx in 0..1000 {
let _ = data.loop_get(idx);
}
})
});

c.bench_function("ref_get", |b| {
let mut data = TernaryTreeList::Empty;

for idx in 0..1000 {
data = data.push(idx)
}

b.iter(|| {
for idx in 0..1000 {
let _ = data.ref_get(idx).to_owned();
}
})
});
}

criterion_group!(benches, criterion_benchmark);
Expand Down
Loading

0 comments on commit 6462f88

Please sign in to comment.