Skip to content

O(n) worst case sampling runtime #21

@LilithHafner

Description

@LilithHafner

The worst case sample time is also O(n) (or O(1) with a large constant factor, if you prefer to think about it that way), achieved when the list of levels starts with a lot of zeros:

julia> using DynamicSampling, Chairmarks

julia> ds = DynamicSampler()
DynamicSampler(Tuple{Int64, Float64}[])

julia> push!(ds, 1, 1.0)
DynamicSampler([(1, 1.0)])

julia> @b ds rand
8.429 ns

julia> for i in 2:1000
           push!(ds, i, 2.0^i)
       end

julia> @b ds rand
16.424 ns

julia> for i in 2:1000
           delete!(ds, i)
       end

julia> @b ds rand
1.292 μs (1 allocs: 48 bytes)

It's possible that this could be fixed by performing a partial sort while traversing the list during sampling, though maybe there's another way with less sampling overhead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions