Skip to content

Instantly share code, notes, and snippets.

@xiaodaigh
Last active January 1, 2018 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiaodaigh/2f7f65b7503068e495a8a94d8b03a738 to your computer and use it in GitHub Desktop.
Save xiaodaigh/2f7f65b7503068e495a8a94d8b03a738 to your computer and use it in GitHub Desktop.
faster uint8,int8,uint16,int16 countmap
const FLOAT16_UNIQS = reinterpret.(Float16, UInt16.(0:2^16-1));
function countmap1(x::Vector{Float16})
arr = zeros(Int, 65536)
for xi in x
@inbounds arr[reinterpret(UInt16, xi)+1] += 1
end
res = Dict{Float16, Int}()
for (i,arr1) in zip(FLOAT16_UNIQS, arr)
isnan(i) || arr1 == 0 || @inbounds res[i] = arr1
end
res
end
function toindex(x::Int16)
Int(x) + 32769
end
function toindex(x::Int8)
Int(x) + 129
end
function toindex(x::T) where T <: Union{UInt8, UInt16}
Int(x) + 1
end
function countmap1(x::Vector{T}) where T <: Union{UInt8,UInt16, Int8, Int16}
arr = zeros(Int, 2^(sizeof(T)*8))
for xi in x
@inbounds arr[toindex(xi)] += 1
end
res = Dict{T, Int}()
for (i,arr1) in zip(typemin(T):typemax(T),arr)
arr1 == 0 || @inbounds res[i] = arr1
end
res
end
using BenchmarkTools, StatsBase
x_uint8 = rand(UInt8, 100_000_000);
@btime countmap1($x_uint8);
@btime countmap($x_uint8);
using BenchmarkTools, StatsBase
x_int8 = rand(Int8, 100_000_000);
@btime countmap1($x_int8);
@btime countmap($x_int8);
using BenchmarkTools, StatsBase
x_int16 = rand(Int16, 100_000_000);
@btime countmap1($x_int16);
@btime countmap($x_int16);
using BenchmarkTools, StatsBase
x_uint16 = rand(UInt16, 100_000_000);
@btime countmap1($x_uint16);
@btime countmap($x_uint16);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment