# 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:** 1\
**Showing post:** 21

<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

```

---

_[View the full topic](https://discourse.julialang.org/t/fastest-way-to-parse-a-string-of-numbers/16720)._
