Coder Social home page Coder Social logo

Comments (6)

Tortar avatar Tortar commented on June 16, 2024 1

mmh, ok but maybe returning nothing could a bit better anyway? It would be easy in this case

function iterate(r::OrdinalRange{T}, i) where {T}
    @inline
    i >= last(r) && return nothing # >= instead of ==
    next = convert(T, i + step(r))
    (next, next)
end

from julia.

KristofferC avatar KristofferC commented on June 16, 2024

What iterate does after nothing has been returned is more or less arbitrary.

from julia.

Tortar avatar Tortar commented on June 16, 2024

I think that anyway the docstring should say that in this case the returned value is arbitrary

from julia.

Tortar avatar Tortar commented on June 16, 2024

Actually the previous implementation wasn't totally correct because of decreasing ranges, this would return nothing whenever the state is outside the range:

function iterate(r::OrdinalRange{T}, i) where {T}
    @inline
    rf, rl = first(r), last(r)
    rf < rl && (i < rf || i >= rl) && return nothing
    rf >= rl && (i <= rl || i > rf) && return nothing
    next = convert(T, i + step(r))
    (next, next)
end

I would have assumed that this is more costly and so probably not worth it but I see on 1.10 and 1.11 this:

julia> function f(x)
           s = 0
           for i in 1:x
               s += rand() < 0.5 ? 1 : 2
           end
       end
f (generic function with 1 method)

julia> @btime f(1000) # before
  581.742 ns (0 allocations: 0 bytes)

julia> @btime f(1000) # after
  489.426 ns (0 allocations: 0 bytes)

which indicates the opposite, does someone know why?

from julia.

Tortar avatar Tortar commented on June 16, 2024

ok sorry I wasn't returning s, now I see same performance

from julia.

Tortar avatar Tortar commented on June 16, 2024

but it is fighting a losing battle trying to make this safer without compromising performance, e.g. still a step greater than 1 should be taken care of

from julia.

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.