34 lines
1.2 KiB
Markdown
34 lines
1.2 KiB
Markdown
# nrange
|
|
The Rust standard library has a Range type, which represents a bounds for
|
|
a scalar value (i.e. 1 ≤ n ≤ 100). A range can be open ended, iterated over,
|
|
and tested for overlap with other ranges. I designed nrange as an extension
|
|
of the Range type to vector space. This makes code less nested. Below is an
|
|
example comparing an operation on a 3D array using standard Ranges and nrange.
|
|
```rust
|
|
let array3 = [[[0; 128]; 128]; 128];
|
|
// Using standard Ranges
|
|
for x in 0..128 {
|
|
for y in 0..128 {
|
|
for z in 0..128 {
|
|
let element = &array3[x][y][z];
|
|
// Do something with element
|
|
}
|
|
}
|
|
}
|
|
// Using nrange
|
|
for [x, y, z] in nrange!(0..128, 0..128, 0..128) {
|
|
let element = &array3[x][y][z];
|
|
// Do something with element
|
|
}
|
|
```
|
|
## Performance
|
|
My implementation is competitive with similar libraries in the Rust
|
|
ecosystem. While nrange only works for ranges of contiguous
|
|
integers, itertools and cartesian-rs work for any iterators.
|
|
However, by restricting my use case I can extract more performance
|
|
gains and integrate better with the Rust standard library. Below
|
|
are benchmark results comparing my solution to comparable libraries:
|
|
* **itertools** ..... 1,821,573 ns
|
|
* **cartesian-rs**... 989,232 ns
|
|
* **nrange** ........ 968,853 ns
|