News for hackers. Stuff that matters.
Re:Julia adoption keeps climbing by jakobnissen on Tuesday January 19, 2021 @01:50AM (#25830714)
In that case, it's closer to C in speed, and usually more generic and "easy on the eyes". I don't know much about C, but seem to recall that it doesn't play well with unicode, usually treating text as bytes. Here's an equivalent Julia example:

    function longest_word(st::Union{String, SubString{String}})
        len = 0
        start = 1
        @inbounds for i in 1:ncodeunits(st)
            if codeunit(st, i) == UInt8(' ')
                len = max(len, i - start)
                start = i + 1
            end
        end
        max(len, ncodeunits(st) + 1 - start)
    end

This takes about 8.2 µs for a 8.5 Kb piece of text on my laptop, but that only works on ASCII text and only treats ' ' as whitespace, not e.g. '\n'. For a more generic one, you can do:

    function longest_word(st::Union{String, SubString{String}})
        len = i = 0
        start = 1
        for char in st
            i += 1
            if isspace(char)
                len = max(len, i - start)
                start = i + 1
            end
        end
        max(len, i + 1 - start)
    end
This is 20 µs for the same text, so still only 3 ns per char. The underlying functionality, namely String iteration and the `isspace` function, is also implemented in pure Julia.

There are no comments on this comment yet.