Skip to content

Commit

Permalink
Add bench_partition_by.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed Dec 23, 2018
1 parent ebca56a commit a2f7de1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
49 changes: 49 additions & 0 deletions benchmark/bench_partition_by.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Statistics
using BenchmarkTools
using Transducers

function manual_partition_by(xs)
acc = zero(eltype(xs))
buffer = similar(xs, 0)
pval = xs[1] > 0
push!(buffer, xs[1])
for x in @view xs[2:end]
val = x > 0
if val == pval
push!(buffer, x)
else
if mean(abs, buffer) < 1.0
acc += prod(buffer)
end
empty!(buffer)
push!(buffer, x)
pval = val
end
end
if mean(abs, buffer) < 1.0
acc += prod(buffer)
end
return acc
end

let suite = BenchmarkGroup()

xf = PartitionBy(x -> x > 0) |>
Filter(xs -> mean(abs, xs) < 1.0) |>
Map(prod)

n = 10^5

let xs = randn(100)
@assert manual_partition_by(xs) == transduce(xf, +, 0.0, xs)
end

suite["xf"] = @benchmarkable(
transduce($xf, +, 0.0, xs),
setup=(xs = randn($n)))
suite["man"] = @benchmarkable(
manual_partition_by(xs),
setup=(xs = randn($n)))

suite
end
1 change: 1 addition & 0 deletions benchmark/benchmarks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ using BenchmarkTools
SUITE = BenchmarkGroup()
SUITE["filter_map_reduce"] = include("bench_filter_map_reduce.jl")
SUITE["filter_map_map!"] = include("bench_filter_map_map!.jl")
SUITE["partition_by"] = include("bench_partition_by.jl")
2 changes: 1 addition & 1 deletion src/library.jl
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ function next(rf::R_{PartitionBy}, result, input)
push!(iinput, input)
else
iresult = next(rf.inner, iresult, iinput)
iinput = InType(rf)[]
empty!(iinput)
isreduced(iresult) || push!(iinput, input)
end
return (iinput, val), iresult
Expand Down
2 changes: 1 addition & 1 deletion test/test_library.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ end

@testset "PartitionBy" begin
# https://clojuredocs.org/clojure.core/partition-by#example-542692c7c026201cdc3269da
xf = PartitionBy(isequal(3))
xf = PartitionBy(isequal(3)) |> Map(copy)
@test collect(xf, 1:5) == [[1, 2], [3], [4, 5]]
end

Expand Down

0 comments on commit a2f7de1

Please sign in to comment.