Setup and teardown in BenchmarkTools

benchmark
#1

I really have the feeling that this is something trivial, since I am doing the same setup/teardown as I found here: https://github.com/Keno/StructIO.jl/issues/13 but that approach does not work to rewind the file before each step.

So I’d like to do some performance studies on parsing binary data and created an MWE:

using BenchmarkTools

struct Foo
    a::Int32
    b::Int32
end

const fname = "test.dat"

write(fname, [Foo(1, 2)])

function read_io(io::IOStream, t::T) where T
    a = read(io, Int32)
    b = read(io, Int32)
    t(a, b)
end

const fobj = open(fname, "r")

@show read_io(fobj, Foo)

seekstart(fobj)

@time read_io(fobj, Foo)

@btime read_io($fobj, Foo) setup=seekstart($fobj)

close(fobj)

This throws an

> julia mwe.jl
read_io(fobj, Foo) = Foo(1, 2)
  0.000005 seconds (5 allocations: 176 bytes)
ERROR: LoadError: EOFError: read end of file
...

Now my simple question: shouldn’t setup=seekstart($fobj) rewind the file before each test? How do I benchmark this?

0 Likes

#2

You likely also need to pass evals = 1 to @btime.

2 Likes