Code Monkey home page Code Monkey logo

Comments (5)

Moelf avatar Moelf commented on June 11, 2024

maybe we should have an unsafe_push!() as the fundamental push so user can manually unlock it and keep pushing if they're just doing single thread work

from fhist.jl.

aminnj avatar aminnj commented on June 11, 2024

Looks like it's searchsortedlast?

function Hist1D(A::AbstractVector, r::AbstractRange{T}) where T <: AbstractFloat
    s = step(r)
    start = first(r)
    start2 = start + 0.5s
    stop = last(r)
    L = length(r) - 1
    counts = zeros(Int, L)
    @inbounds for idx in eachindex(A)
        # default
        i = A[idx]
        c = ifelse(i > stop, 0, 1)
        c = ifelse(i < start, 0, c)
        id = round(Int, (i - start2) / s) + 1
        counts[clamp(id, 1, L)] += c

        # push!() uses `searchsorted`
        # i = A[idx]
        # id = searchsortedlast(r, i)
        # c = ifelse(i > stop, 0, 1)
        # c = ifelse(i < start, 0, c)
        # counts[id] += c

    end
    return Hist1D(Histogram(r, counts))
end

The above took 6.5ms with the default implementation, but 45ms with searchsortedlast plugged in (as is used for push!().

from fhist.jl.

Moelf avatar Moelf commented on June 11, 2024

I thought searchsortedlast is fast over ranges as well

from fhist.jl.

aminnj avatar aminnj commented on June 11, 2024

That's what I thought from profiling...
image
there's some time spent in computing the bin index (good, no binary search), but those percentages don't seem to add up to
image

from fhist.jl.

aminnj avatar aminnj commented on June 11, 2024

And fwiw, above, the manual calculation matches the searchsortedlast for 1e6 events, so we could factor out

@inline function calcbinindex(r::AbstractRange{<:Real}, x::Real)
    s = step(r)
    start2 = first(r) + 0.5s
    return round(Int, (x - start2) / s) + 1
end

and use it in place of searchsortedlast everywhere

from fhist.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.