Fast check if a character "is" an integer

Sorry you’re right, I confirmed with @btime (and @code_native, the code is seemingly not optimized away), unlike with @time. It was seemingly since not wrapped in a function, then @time showed none.

Can you explain the difference here?

julia> a = ""
""

julia> @btime all(x -> 0x30 <= x <= 0x39, codeunits(a))
  171.304 ns (2 allocations: 32 bytes)
true

julia> @btime all(x -> 0x30 <= x <= 0x39, codeunits(""))
  6.400 ns (0 allocations: 0 bytes)
true

Can you explain this? The @code_lowered code looks the same.

It’s a benchmarking artefact, due to a being a non-const global. You can fix it like this:

julia> a = "";

julia> @btime all(x -> 0x30 <= x <= 0x39, codeunits($a))
  7.700 ns (0 allocations: 0 bytes)
true

or like this:

julia> const b = "";

julia> @btime all(x -> 0x30 <= x <= 0x39, codeunits(b))
  7.608 ns (0 allocations: 0 bytes)
true
1 Like

In that case no actual allocations as already explained, but one reason could have been only some code-paths allocate, and testing can only show allocations, not absence of potential allocations.

We need a macro @nogc similar to that annotation in Dlang, to force functions to not allocate (nor call functions that do), but I believe there are ways to know your code can’t allocate for sure (and most often 0 allocations are a good indicator of that).