# Copying bytes from AbstractVector to Ptr

**URL:** <https://discourse.julialang.org/t/copying-bytes-from-abstractvector-to-ptr/119408>\
**Category:** Performance\
**Created:** [September 14, 2024, 4:57pm UTC](https://discourse.julialang.org/t/copying-bytes-from-abstractvector-to-ptr/119408 "2024-09-14T16:57:49Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [September 14, 2024, 6:18pm UTC](https://discourse.julialang.org/t/copying-bytes-from-abstractvector-to-ptr/119408/7 "2024-09-14T18:18:52Z")

</div>

If you want to have it faster, you can use `@inbounds`. Ignoring manual bounds checking, that gives

```julia
function unsafe_read3!(
        dest::Ptr{T},
        src::AbstractVector{T},
        so::Integer,
        nbytes::Integer) where T
    for i in 1:nbytes
        unsafe_store!(dest, @inbounds(src[so+i-1]), i)
    end
end

```

With `nbytes = 4000`, I get

```julia
julia> @b unsafe_read1!($d, $s, 1, $nbytes)
2.992 μs (1 allocs: 4.062 KiB)

julia> @b unsafe_read3!($d, $s, 1, $nbytes)
1.845 μs

```

---

_[View the full topic](https://discourse.julialang.org/t/copying-bytes-from-abstractvector-to-ptr/119408)._
