# Non-allocating way to parse section of string as number?

**URL:** <https://discourse.julialang.org/t/non-allocating-way-to-parse-section-of-string-as-number/137672>\
**Category:** Performance\
**Tags:** question, strings, parsing\
**Created:** [June 17, 2026, 8:05pm UTC](https://discourse.julialang.org/t/non-allocating-way-to-parse-section-of-string-as-number/137672 "2026-06-17T20:05:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)\
**Post date:** [June 17, 2026, 8:05pm UTC](https://discourse.julialang.org/t/non-allocating-way-to-parse-section-of-string-as-number/137672/1 "2026-06-17T20:05:55Z")

</div>

Given a big string with numbers in it like

```julia-auto
input = ";(,)<mul(595,110)~ #(select()-?who():mul(732,729)+/;%@mul(924,700)"

```

and I know at execution time exactly where a number is in the string, I can parse that section of the string like so

```julia-auto
i = 10
parse(Int, input[i:i+2])

```

but this allocates

```julia-auto
Benchmark: 2605 samples with 1264 evaluations
 min 19.896 ns (1 allocs: 32 bytes)
 median 23.248 ns (1 allocs: 32 bytes)
 mean 29.374 ns (1 allocs: 32 bytes, 0.08% gc time)
 max 7.418 μs (1 allocs: 32 bytes, 99.26% gc time)

```

which can (in my experiments) easily add up over a very big string and cause a slow down.

My solution was to just implement a very naive parser that doesn’t allocate

```julia-auto
arr = codeunits(input) #outside the call to naiveparseint and used many times
function naiveparseint(arr, xstart, xend)
	output::Int = 0
	pow = 0
	for i in xend:-1:xstart
		output += (arr[i]-0x30)*10^pow
		pow += 1
	end
	return output
end

```

but it feels like there must be a better way to do this in Julia.

Any ideas?

---

<div class="post-metadata">

**Author:** ![nhz2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhz2/32/44428_2.png) [@nhz2](https://discourse.julialang.org/u/nhz2)\
**Post date:** [June 17, 2026, 8:15pm UTC](https://discourse.julialang.org/t/non-allocating-way-to-parse-section-of-string-as-number/137672/2 "2026-06-17T20:15:18Z")

</div>

Instead of `pow = 0`, `*10^pow`, `pow +=1`, you can do `mult = 1`, `*mult`, `mult *= 10`

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [June 17, 2026, 8:31pm UTC](https://discourse.julialang.org/t/non-allocating-way-to-parse-section-of-string-as-number/137672/3 "2026-06-17T20:31:57Z")

</div>

Try

```julia-auto
parse(Int, @view input[i:i+2])

```
