# How can i convert number string array to int array

**URL:** https://discourse.julialang.org/t/how-can-i-convert-number-string-array-to-int-array/32536
**Category:** General Usage
**Created:** [December 21, 2019, 4:42am UTC](https://discourse.julialang.org/t/how-can-i-convert-number-string-array-to-int-array/32536 "2019-12-21T04:42:14Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![imrankhan\_juliaLang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imrankhan_julialang/32/12020_2.png) [@imrankhan\_juliaLang](https://discourse.julialang.org/u/imrankhan_juliaLang)
#### Post date: [December 21, 2019, 4:42am UTC](https://discourse.julialang.org/t/how-can-i-convert-number-string-array-to-int-array/32536/1 "2019-12-21T04:42:14Z")

</div>

Need help to convert string number array to int array

**Like**  
[“0”, “1”, “0”, “1”, “0”, “1”, “1”, “1”] **To** [0, 1, 0, 1, 0, 1, 1, 1]

**NB:** Without use this code  
`[parse(Int, x) for x in array]`  
because this method is too slow in big array **[array size about 1 million +]**

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [December 21, 2019, 5:51am UTC](https://discourse.julialang.org/t/how-can-i-convert-number-string-array-to-int-array/32536/2 "2019-12-21T05:51:08Z")

</div>

You could potentially convert it to a categorical array first.

---

<div class="post-metadata">

### Author: ![Syx\_Pek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/syx_pek/32/6364_2.png) [@Syx\_Pek](https://discourse.julialang.org/u/Syx_Pek)
#### Post date: [December 21, 2019, 7:27am UTC](https://discourse.julialang.org/t/how-can-i-convert-number-string-array-to-int-array/32536/3 "2019-12-21T07:27:03Z")

</div>

Is it really a problem?

```julia
julia> A = [string(rand(1:1000)) for _ in 1:1_000_000];

julia> @btime parse.(Int, $A);
  27.091 ms (2 allocations: 7.63 MiB)

```

---

<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: [December 21, 2019, 9:03am UTC](https://discourse.julialang.org/t/how-can-i-convert-number-string-array-to-int-array/32536/4 "2019-12-21T09:03:49Z")

</div>

It seems like these two methods are equally fast:

```julia
julia> a = rand(("0", "1"), 10^6);

julia> @btime [parse(Int, x) for x in $a];
  36.087 ms (3 allocations: 7.63 MiB)

julia> @btime parse.(Int, $a);
  35.576 ms (2 allocations: 7.63 MiB)

```

It seems pretty fast to me.

If, however, the inputs are `Char`s instead of `String`s, it can get faster:

```julia
julia> c = rand('0':'1', 10^6);

julia> @btime parse.(Int, $c);
  8.302 ms (2 allocations: 7.63 MiB)

```

or even

```julia
julia> @btime Int.($c) .- 48;
  2.241 ms (2 allocations: 7.63 MiB)

```

Here’s another one, if `Int8` is ok:

```julia
julia> @btime Int8.($c) .- Int8(48);
  784.793 μs (2 allocations: 976.70 KiB)

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [December 21, 2019, 10:50am UTC](https://discourse.julialang.org/t/how-can-i-convert-number-string-array-to-int-array/32536/5 "2019-12-21T10:50:14Z")

</div>

If you don’t need to validate inputs and these are the only two options, something like

```julia
parse01(str) = str == "1" ? 1 : 0

```

could work, eg

```julia
julia> @btime parse01.($a);
  4.704 ms (2 allocations: 7.63 MiB)

```

but, like the others contributing to this topic, I wonder if this is really the bottleneck in your code. I would go with `parse(Int, ...)` as the cost is trivial in any case, and it is robust and readable.
