https://github.com/tkoolen/fastiobuffers.jl
Faster alternatives to Julia's IOBuffer type
https://github.com/tkoolen/fastiobuffers.jl
Last synced: about 2 months ago
JSON representation
Faster alternatives to Julia's IOBuffer type
- Host: GitHub
- URL: https://github.com/tkoolen/fastiobuffers.jl
- Owner: tkoolen
- License: other
- Created: 2018-08-07T13:58:07.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-02-12T13:31:20.000Z (about 5 years ago)
- Last Synced: 2025-02-22T06:02:36.372Z (2 months ago)
- Language: Julia
- Homepage:
- Size: 22.5 KB
- Stars: 6
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# FastIOBuffers
[](https://travis-ci.org/tkoolen/FastIOBuffers.jl)
[](http://codecov.io/github/tkoolen/FastIOBuffers.jl?branch=master)FastIOBuffers aims to provide faster alternatives to `Base.IOBuffer`, which as of time of writing allocates memory even when e.g. a `Float64` is written to or read from it.
### FastWriteBuffer
`FastWriteBuffer` solves the allocation problem for the write use case. On 1.1.0, using `IOBuffer`:
```julia
using BenchmarkTools
const N = 1000
@btime write(buf, x) evals = N setup = begin
x = rand(Float64)
buf = IOBuffer(Vector{UInt8}(undef, N * Core.sizeof(x)), read=false, write=true)
end
```results in `15.582 ns (1 allocation: 16 bytes)`, while
```julia
using BenchmarkTools
using FastIOBuffers
const N = 1000
@btime write(buf, x) evals = N setup = begin
x = rand(Float64)
buf = FastWriteBuffer(Vector{UInt8}(undef, N * Core.sizeof(x)))
end
```results in `10.759 ns (0 allocations: 0 bytes)`
### FastReadBuffer
Similarly, `FastReadBuffer` can be used in place of `IOBuffer` for reading. On 1.1.0, using `IOBuffer`:
```julia
using BenchmarkTools, Random
const N = 1000
@btime read(buf, Float64) evals = N setup = begin
rng = MersenneTwister(1)
writebuf = IOBuffer()
map(1 : N) do _
write(writebuf, rand(rng, Float64))
end
buf = IOBuffer(take!(writebuf))
end
```results in `3.368 ns (0 allocations: 0 bytes)`, while replacing `IOBuffer` with `FastReadBuffer` results in `1.344 ns (0 allocations: 0 bytes)`.