Skip to content

Commit d14dcb2

Browse files
committed
chore: add enum benches
1 parent b820599 commit d14dcb2

4 files changed

Lines changed: 25 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Early-exit search: lazy enumerate avoids allocating tuples
2+
// for elements past the target.
3+
let xs = list(0..1_000_000);
4+
let target = 750_000;
5+
let found = xs.enumerate().find(fn(p) => p[1] == target);
6+
print(found);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Common pattern: iterate every element with its index.
2+
// Lazy version avoids allocating an intermediate list of tuples.
3+
let xs = list(0..500_000);
4+
let acc = 0;
5+
for (i, v) in xs.enumerate() {
6+
acc += i + v;
7+
}
8+
print(acc);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Lazy enumerate should win big: only the first 10 tuples are needed,
2+
// but the eager version allocates a full Vec of 1_000_000 tuples first.
3+
let xs = list(0..1_000_000);
4+
let head = xs.enumerate().take(10).list();
5+
print(head.len);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Worst case for lazy: full materialization to a list.
2+
// Lazy adds one indirection layer per element; expect a small regression
3+
// or near-parity here.
4+
let xs = list(0..500_000);
5+
let result = xs.enumerate().list();
6+
print(result.len);

0 commit comments

Comments
 (0)