# Fast check if a character "is" an integer

**URL:** <https://discourse.julialang.org/t/fast-check-if-a-character-is-an-integer/88750>\
**Category:** Performance\
**Created:** [October 14, 2022, 7:20pm UTC](https://discourse.julialang.org/t/fast-check-if-a-character-is-an-integer/88750 "2022-10-14T19:20:40Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [October 18, 2022, 12:07pm UTC](https://discourse.julialang.org/t/fast-check-if-a-character-is-an-integer/88750/22 "2022-10-18T12:07:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jake](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jake/32/46007_2.png) [@Jake](https://discourse.julialang.org/u/Jake)\
**Post date:** [October 18, 2022, 12:18pm UTC](https://discourse.julialang.org/t/fast-check-if-a-character-is-an-integer/88750/23 "2022-10-18T12:18:34Z")

</div>

> [@DNF](#):
>
> this I cannot reproduce:

Can you explain the difference here?

```julia
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.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 18, 2022, 12:22pm UTC](https://discourse.julialang.org/t/fast-check-if-a-character-is-an-integer/88750/24 "2022-10-18T12:22:06Z")

</div>

> [@Jake](#):
>
> `@btime all(x -> 0x30 <= x <= 0x39, codeunits(a))`

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

```julia
julia> a = "";

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

```

or like this:

```julia
julia> const b = "";

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

```

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [October 18, 2022, 12:28pm UTC](https://discourse.julialang.org/t/fast-check-if-a-character-is-an-integer/88750/25 "2022-10-18T12:28:38Z")

</div>

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).

[Previous page](https://discourse.julialang.org/t/fast-check-if-a-character-is-an-integer/88750.md?page=1)
