Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #=
- # follows code by Jacob Quinn in response to this
- t = zeros(Int64, 100000);
- @btime sum(t)
- 12.084 μs (0 allocations: 0 bytes)
- 0
- t2 = reinterpret(Int64, zeros(UInt64, 100000));
- @btime sum(t2)
- 88.805 μs (0 allocations: 0 bytes)
- 0
- =#
- using BenchmarkTools
- struct TVector{T, S} <: AbstractVector{T}
- x::Vector{S}
- end
- Base.size(a::TVector) = size(a.x)
- Base.IndexStyle(::Type{T}) where {T <: TVector} = Base.IndexLinear();
- @inline function Base.getindex(A::TVector{T}, i::Int) where {T}
- @boundscheck checkbounds(A, i)
- @inbounds x = Core.bitcast(T, A.x[i])
- return x
- end
- function fast_reinterpret(::Type{T}, x::Array{V,1}) where {V,T}
- (sizeof(T) !== sizeof(V) || !(isbitstype(T) && isbitstype(V))) &&
- throw(ErrorException("Cannot reinterpret($T, Vector{$V})"))
- return TVector{T, V}(x)
- end
- #=
- ivec = ones(Int64, 1_000_000);
- uvec = reinterpret(UInt64, ivec);
- q = reinterpret(UInt64, ivec); eltype(q) === UInt64
- q = reinterpret(Float64, ivec); eltype(q) === Float64
- @btime sum(reinterpret(UInt64, ivec));
- @btime sum(fast_reinterpret(UInt64, ivec));
- uvec_base = reinterpret(UInt64, ivec);
- uvec_fast = fast_reinterpret(UInt64, ivec);
- elapsed_base = @belapsed sum(uvec_base);
- elapsed_fast = @belapsed sum(uvec_fast);
- relative_speed = round(elapsed_base / elapsed_fast, digits=1)
- 8.8
- =#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement