# How can i convert number string array to int array

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 +]

You could potentially convert it to a categorical array first.

1 Like

Is it really a problem?

``````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)
``````
3 Likes

It seems like these two methods are equally fast:

``````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> c = rand('0':'1', 10^6);

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

or even

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

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

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

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

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

could work, eg

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