-
-
Save xiaodaigh/2f7f65b7503068e495a8a94d8b03a738 to your computer and use it in GitHub Desktop.
faster uint8,int8,uint16,int16 countmap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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