scalanlp/breeze — pull request #267 by stucchio (2014-06-29 06:33:59+00:00)
Title: Minor performance improvements for Rand.condition

Added some benchmarks. Before:

[info] Running breeze.linalg.ProbMonadRunner
[info] 0% Scenario{vm=java, trial=0, benchmark=Map} 133255769.33 ns; σ=1180506.86 ns @ 3 trials
[info] 25% Scenario{vm=java, trial=0, benchmark=FlatMap} 114747129.08 ns; σ=1142942.59 ns @ 6 trials
[info] 50% Scenario{vm=java, trial=0, benchmark=Condition} 163731247.33 ns; σ=1173484.00 ns @ 3 trials
[info] 75% Scenario{vm=java, trial=0, benchmark=RepeatCondition} 307967589.50 ns; σ=1495141.27 ns @ 3 trials
[info]
[info] benchmark ms linear runtime
[info] Map 133 ============
[info] FlatMap 115 ===========
[info] Condition 164 ===============
[info] RepeatCondition 308 ==============================

After:

[info] Running breeze.linalg.ProbMonadRunner
[info] 0% Scenario{vm=java, trial=0, benchmark=Map} 133609547.43 ns; σ=1262758.71 ns @ 10 trials
[info] 25% Scenario{vm=java, trial=0, benchmark=FlatMap} 115288567.29 ns; σ=773792.55 ns @ 3 trials
[info] 50% Scenario{vm=java, trial=0, benchmark=Condition} 162809875.22 ns; σ=595432.81 ns @ 3 trials
[info] 75% Scenario{vm=java, trial=0, benchmark=RepeatCondition} 277582390.40 ns; σ=1227784.94 ns @ 3 trials
[info]
[info] benchmark ms linear runtime
[info] Map 134 ==============
[info] FlatMap 115 ============
[info] Condition 163 =================
[info] RepeatCondition 278 ==============================

The improvement comes in condition - rather than having a nested sequence of inner classes each of which tracks a single predicate, we just have one class that stores a list of the predicates. When we add new conditions, we just add to the list of predicates.