# Fastest way to parse a string of numbers

**URL:** <https://discourse.julialang.org/t/fastest-way-to-parse-a-string-of-numbers/16720>\
**Category:** General Usage\
**Tags:** question, parsing\
**Created:** [October 24, 2018, 8:23am UTC](https://discourse.julialang.org/t/fastest-way-to-parse-a-string-of-numbers/16720 "2018-10-24T08:23:09Z")\
**Posts on this page:** 2\
**Page:** 2

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [November 6, 2021, 2:56am UTC](https://discourse.julialang.org/t/fastest-way-to-parse-a-string-of-numbers/16720/21 "2021-11-06T02:56:22Z")

</div>

You should look into using `Parsers.xparse` directly, since it will avoid some overhead of using `Parsers.parse`. It also respects a `Parsers.Options` struct where you can pass the `' '` space delimiter which will be consumed automatically, as well as handling newlines. You could check out a recent example of how to do this in the upcoming [PowerFlowData.jl](https://github.com/nickrobinson251/PowerFlowData.jl/blob/5bbeeef6fe5e1a248ab9b68d04c3a43b9054a7ef/src/parsing.jl#L138) package. Basically, `Parsers.xparse` is what `Parsers.parse` calls under the hood. It’s most efficient if you pass it a raw vector of bytes, either by calling `read(filepath)` or `Mmap.mmap(filepath)`. You get back a `Parsers.Result{T}` object from calling `Parsers.xparse` which gives you a `code` which will signify if parsing succeeded, if a newline was encountered, etc; a `val`, which is the actual parsed value, and `tlen`, which is the total number of bytes consumed while parsing (including any delimiters). So the general usage is like:

```julia
function parsestuff(file)
    buf = read(file)
    len = length(buf)
    pos = 1
    opts = Parsers.Options(delim=' ', wh1=0x00)
    while pos <= len
        res = Parsers.xparse(Int, buf, pos, len, opts)
        if Parsers.ok(res.code)
            # parsing succeeded, do stuff with res.val
        end
        pos += res.tlen
    end
end

```

---

<div class="post-metadata">

**Author:** ![Cristopher\_Moen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cristopher_moen/32/20969_2.png) [@Cristopher\_Moen](https://discourse.julialang.org/u/Cristopher_Moen)\
**Post date:** [April 29, 2022, 9:07pm UTC](https://discourse.julialang.org/t/fastest-way-to-parse-a-string-of-numbers/16720/22 "2022-04-29T21:07:44Z")

</div>

```julia
opts = Parsers.Options(delim=' ', ignorerepeated=true, wh1=0x00)
    io = IOBuffer("1.0 2.34345 7.9")
    Parsers.parse(Float64, io, opts) # returns 1.0
    Parsers.parse(Float64, io, opts) # returns 2.34345
    Parsers.parse(Float64, io, opts) # returns 7.9

```

This is not working with the v2.3.1 update! I will try to figure out why not. Some help is appreciated.

[Previous page](https://discourse.julialang.org/t/fastest-way-to-parse-a-string-of-numbers/16720.md?page=1)
