结果在我的 M2 Mac mini 上,
- 有边界检查: 1152 ms
- 无边界检查: 1084 ms
基本是 6% 左右的时间开销(不确定我这个封装是否有额外开销).
附源代码:
```rust
struct Array<T>(*mut T);
impl<T> From<*const T> for Array<T> {
fn from(ptr: *const T) -> Self {
Self(ptr as *mut _)
}
}
impl<T> std::ops::Index<usize> for Array<T> {
type Output = T;
fn index(&self, index: usize) -> &Self::Output {
unsafe {
let ptr = self.0.offset(index as isize);
&*ptr
}
}
}
impl<T> std::ops::IndexMut<usize> for Array<T> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
unsafe {
let ptr = self.0.offset(index as isize);
&mut *ptr
}
}
}
fn main() {
const SIZE: usize = 1024 * 1024;
const LOOP: usize = 2_000_000;
let mut arr = vec![0u32; SIZE];
let start = std::time::Instant::now();
// array indexing with boundary check
{
for _ in 0..LOOP {
let index = rand::random::<usize>() % SIZE;
arr[index] += 1;
}
}
let elapsed = start.elapsed();
println!("Array indexing with boundary check runtime: {}ms", elapsed.as_millis());
// to avoid cache, use a different raw array.
let mut arr = Array::from(vec![0u32; SIZE].as_ptr());
let start = std::time::Instant::now();
// array indexing wthout boundary check
{
for _ in 0..LOOP {
let index = rand::random::<usize>() % SIZE;
arr[index] += 1;
}
}
let elapsed = start.elapsed();
println!("Array indexing without boundary check runtime: {}ms", elapsed.as_millis());
}
``` |
|