From f6f07f817077f1e6d6e22348a7af6e8494650738 Mon Sep 17 00:00:00 2001 From: Twan Koolen Date: Fri, 3 Aug 2018 21:21:27 -0400 Subject: [PATCH 1/2] Fix readstring benchmark, add readfloat64. --- src/io/IOBenchmarks.jl | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/io/IOBenchmarks.jl b/src/io/IOBenchmarks.jl index 7ec44dbb..d3c56b19 100644 --- a/src/io/IOBenchmarks.jl +++ b/src/io/IOBenchmarks.jl @@ -19,21 +19,20 @@ const SUITE = BenchmarkGroup() # read (#12364) # ################# -function perf_read!(io) +function perf_read!(io, ::Type{T}) where T seekstart(io) - x = 0 + x = zero(T) while !(eof(io)) - x += read(io, UInt8) + x += read(io, T) end return x end g = addgroup!(SUITE, "read", ["buffer", "stream", "string"]) -testbuf = IOBuffer(randstring(RandUtils.SEED, 10^4)) - -g["read"] = @benchmarkable perf_read!($testbuf) -g["readstring"] = @benchmarkable read($testbuf, String) +g["read"] = @benchmarkable perf_read!(testbuf, UInt8) setup = testbuf = IOBuffer(randstring(RandUtils.SEED, 10^4)) +g["readfloat64"] = @benchmarkable perf_read!(testbuf, Float64) setup = testbuf = IOBuffer(randstring(RandUtils.SEED, 10^4)) +g["readstring"] = @benchmarkable read(testbuf, String) setup = testbuf = IOBuffer(randstring(RandUtils.SEED, 10^4)) ################################# # serialization (#18633, #7893) # From c5dc1b8232aaca92aeff122cda3d8390978a6f0e Mon Sep 17 00:00:00 2001 From: Twan Koolen Date: Mon, 6 Aug 2018 11:14:36 -0400 Subject: [PATCH 2/2] Fix readstring benchmark properly, reuse testbuf again. `perf_read!` does a `seekstart` at the beginning, but `perf_read!` was not used for the `readstring` benchmark, only the `read` benchmark. So if the `read` benchmark was done before the `readstring` benchmark, `testbuf` would definitely be at eof, as witnessed by the too-good-to-be-true 80 ns benchmark time. The implementation in the previous commit was also incorrect, as there can be multiple evaluations per sample. Also reuse `testbuf` again in response to https://github.com/JuliaCI/BaseBenchmarks.jl/pull/223#issuecomment-410464560. --- src/io/IOBenchmarks.jl | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/io/IOBenchmarks.jl b/src/io/IOBenchmarks.jl index d3c56b19..3b70b25b 100644 --- a/src/io/IOBenchmarks.jl +++ b/src/io/IOBenchmarks.jl @@ -19,7 +19,7 @@ const SUITE = BenchmarkGroup() # read (#12364) # ################# -function perf_read!(io, ::Type{T}) where T +function perf_read!(io, ::Type{T}) where T<:Number seekstart(io) x = zero(T) while !(eof(io)) @@ -28,11 +28,17 @@ function perf_read!(io, ::Type{T}) where T return x end +function perf_read!(io, ::Type{String}) + seekstart(io) + read(io, String) +end + g = addgroup!(SUITE, "read", ["buffer", "stream", "string"]) -g["read"] = @benchmarkable perf_read!(testbuf, UInt8) setup = testbuf = IOBuffer(randstring(RandUtils.SEED, 10^4)) -g["readfloat64"] = @benchmarkable perf_read!(testbuf, Float64) setup = testbuf = IOBuffer(randstring(RandUtils.SEED, 10^4)) -g["readstring"] = @benchmarkable read(testbuf, String) setup = testbuf = IOBuffer(randstring(RandUtils.SEED, 10^4)) +testbuf = IOBuffer(randstring(RandUtils.SEED, 10^4)) +g["read"] = @benchmarkable perf_read!($testbuf, UInt8) +g["readfloat64"] = @benchmarkable perf_read!($testbuf, Float64) +g["readstring"] = @benchmarkable perf_read!($testbuf, String) ################################# # serialization (#18633, #7893) #