# Quickest method to convert Vector of Strings to 3 Vectors of Floats

**URL:** <https://discourse.julialang.org/t/quickest-method-to-convert-vector-of-strings-to-3-vectors-of-floats/31522>\
**Category:** General Usage\
**Created:** [November 26, 2019, 9:09am UTC](https://discourse.julialang.org/t/quickest-method-to-convert-vector-of-strings-to-3-vectors-of-floats/31522 "2019-11-26T09:09:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![scelles](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scelles/32/220408_2.png) [@scelles](https://discourse.julialang.org/u/scelles)\
**Post date:** [November 26, 2019, 9:09am UTC](https://discourse.julialang.org/t/quickest-method-to-convert-vector-of-strings-to-3-vectors-of-floats/31522/1 "2019-11-26T09:09:50Z")

</div>

Hello,

I have a vector of String like

```
a = ["0.309042 46.586349 111.4", "0.309042 46.586349 111.1", "0.309042 46.586349 111.3"]

```

and want to get 3 vectors of coordinates (longitude, latitude, altitude) (Float64)

I did

```
a = split.(a)
longitude = [parse(Float64, c[1]) for c in a]
latitude = [parse(Float64, c[2]) for c in a]
altitude = [parse(Float64, c[3]) for c in a]

```

I wonder if there isn’t a quicker method

```julia
function timethis()
    a = ["0.309042 46.586349 111.4", "0.309042 46.586349 111.1", "0.309042 46.586349 111.3"]
    a = split.(a)
    longitude = [parse(Float64, c[1]) for c in a]
    latitude = [parse(Float64, c[2]) for c in a]
    altitude = [parse(Float64, c[3]) for c in a]
    return longitude, latitude, altitude
end

julia> @time timethis()
  0.407742 seconds (237.58 k allocations: 11.579 MiB, 39.14% gc time)
([0.309042, 0.309042, 0.309042], [46.586349, 46.586349, 46.586349], [111.4, 111.1, 111.3])

```

Kind regards

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [November 26, 2019, 10:30am UTC](https://discourse.julialang.org/t/quickest-method-to-convert-vector-of-strings-to-3-vectors-of-floats/31522/2 "2019-11-26T10:30:44Z")

</div>

I am not sure if there is a faster approach, but there are some issues with your timing.  
It seems that you are mainly timing compile time:

```julia
julia> function timethis()
           a = ["0.309042 46.586349 111.4", "0.309042 46.586349 111.1", "0.309042 46.586349 111.3"]
           a = split.(a)
           longitude = [parse(Float64, c[1]) for c in a]
           latitude = [parse(Float64, c[2]) for c in a]
           altitude = [parse(Float64, c[3]) for c in a]
           return longitude, latitude, altitude
       end
timethis (generic function with 1 method)

julia> @time timethis()
  0.118533 seconds (436.11 k allocations: 21.948 MiB, 3.99% gc time)
([0.309042, 0.309042, 0.309042], [46.586349, 46.586349, 46.586349], [111.4, 111.1, 111.3])

julia> @time timethis()
  0.000018 seconds (37 allocations: 1.719 KiB)
([0.309042, 0.309042, 0.309042], [46.586349, 46.586349, 46.586349], [111.4, 111.1, 111.3])

```

To benchmark your function try this:

```julia
julia> using BenchmarkTools

julia> a = ["0.309042 46.586349 111.4", "0.309042 46.586349 111.1", "0.309042 46.586349 111.3"]

julia> @benchmark timethis($a)
BenchmarkTools.Trial:
  memory estimate: 1.45 KiB
  allocs estimate: 32
  --------------
  minimum time: 6.980 μs (0.00% GC)
  median time: 7.180 μs (0.00% GC)
  mean time: 9.269 μs (12.97% GC)
  maximum time: 9.537 ms (99.88% GC)
  --------------
  samples: 10000
  evals/sample: 5

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [November 27, 2019, 4:48pm UTC](https://discourse.julialang.org/t/quickest-method-to-convert-vector-of-strings-to-3-vectors-of-floats/31522/3 "2019-11-27T16:48:32Z")

</div>

Here are some more examples on how to do it in other ways:

> [@Best way to get all substrings or numbers matching a regex](https://discourse.julialang.org/t/best-way-to-get-all-substrings-or-numbers-matching-a-regex/31571):
>
> Bonjour, What is the best way for getting a String on Number Vector from a regex (like with the old matchall method)? pat = r"[+-]?\d+\.?\d\*" txt = "aaa -1 bbb +2.2 ccc 123.456 ddd" # findall? ranges = findall(pat, txt) words = map(range-\>txt[range], ranges) # could be shorter? numbers = parse.(Float64, words) # =\> # 3-element Array{Float64,1}: # 1.0 # -1.0 # 123.456 # eachmatch? matches = eachmatch(pat, txt) words = getfield.(matches, :match) numbers = parse.(Float64, words) # =\>…

(i case you don’t follow the discourse)
