跳转到内容
彼岸论坛
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[Rust] 试图通过 unsafe 绕过越界检查看越界检查有多少额外开销


已推荐帖子

发表于

结果在我的 M2 Mac mini 上,

  • 有边界检查: 1152 ms
  • 无边界检查: 1084 ms

基本是 6% 左右的时间开销(不确定我这个封装是否有额外开销).

附源代码:

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());
    
}
  • 游客注册

    游客注册

  • 会员

    没有会员可显示

  • 最新的状态更新

    没有最新的状态更新
  • 最近查看

    • 没有会员查看此页面.
×
×
  • 创建新的...