Created
May 26, 2014 20:55
-
-
Save benoitx/f4248c1419b812f77fd6 to your computer and use it in GitHub Desktop.
BigDecimal Sum Play
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigDecimal; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.Collections; | |
import java.util.EnumSet; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Set; | |
import java.util.function.BiConsumer; | |
import java.util.function.BinaryOperator; | |
import java.util.function.Function; | |
import java.util.function.Supplier; | |
import java.util.stream.Collector; | |
import net.objectlab.kit.util.Total; | |
import com.equatesecurities.oms.util.BigDecimalAccumulator; | |
import com.jamonapi.Monitor; | |
import com.jamonapi.MonitorFactory; | |
import static java.util.stream.Collector.Characteristics.CONCURRENT; | |
import static java.util.stream.Collector.Characteristics.IDENTITY_FINISH; | |
public class BigDecimalPlay { | |
private static class Test { | |
private final BigDecimal value; | |
public Test() { | |
value = null; | |
} | |
public Test(final int value) { | |
super(); | |
this.value = BigDecimal.valueOf(value); | |
} | |
public BigDecimal getValue() { | |
return value; | |
} | |
} | |
public static void main(final String[] args) { | |
final List<Test> list = new ArrayList<>(); | |
System.out.println("Prepare"); | |
for (int i = 0; i < 50000; i++) { | |
list.add(new Test(i)); | |
} | |
list.add(new Test()); | |
System.out.println("Go"); | |
for (int i = 0; i < 1000; i++) { | |
System.out.println(i + "--"); | |
useTotal(list); | |
useTotalAndForEach(list); | |
useTotalAndMap(list); | |
useMapAndReduce(list); | |
useReduceAndAccumulator(list); | |
useCollector(list); | |
useCalculator(list); | |
useParallelCollector(list); | |
} | |
System.out.println("done"); | |
} | |
private static void useCalculator(final List<Test> list) { | |
final Monitor mon = MonitorFactory.start("Total+calc"); | |
final BigDecimal reduce = Calculator.sum(list, Test::getValue); | |
mon.stop(); | |
System.out.printf("%10.0f in %13s %5.1f Avg:%5.1f Min:%5.1f Max:%5.1f %n", reduce, mon.getLabel(), mon.getLastValue(), mon.getAvg(), | |
mon.getMin(), mon.getMax()); | |
} | |
private static class Calculator { | |
public static <T> BigDecimal sum(final Collection<T> collection, final Function<T, BigDecimal> mapper) { | |
return collection.stream().map(mapper).reduce(BigDecimal.ZERO, (l, r) -> r != null ? l.add(r) : l); | |
} | |
} | |
private static void useParallelCollector(final List<Test> list) { | |
final Monitor mon = MonitorFactory.start("Total+collecP"); | |
final BigDecimal reduce = list.parallelStream().map(Test::getValue).collect(new ToTotalCollector()).getTotal(); | |
mon.stop(); | |
System.out.printf("%10.0f in %13s %5.1f Avg:%5.1f Min:%5.1f Max:%5.1f %n", reduce, mon.getLabel(), mon.getLastValue(), mon.getAvg(), | |
mon.getMin(), mon.getMax()); | |
} | |
private static void useCollector(final List<Test> list) { | |
final Monitor mon = MonitorFactory.start("Total+collect"); | |
final BigDecimal reduce = list.stream().map(Test::getValue).collect(new ToTotalCollector()).getTotal(); | |
mon.stop(); | |
System.out.printf("%10.0f in %13s %5.1f Avg:%5.1f Min:%5.1f Max:%5.1f %n", reduce, mon.getLabel(), mon.getLastValue(), mon.getAvg(), | |
mon.getMin(), mon.getMax()); | |
} | |
private static void useMapAndReduce(final List<Test> list) { | |
final Monitor mon = MonitorFactory.start("Total+reduce1"); | |
final BigDecimal reduce = list.stream().map(Test::getValue).reduce(BigDecimal.ZERO, (a, b) -> b != null ? a.add(b) : a); | |
mon.stop(); | |
System.out.printf("%10.0f in %13s %5.1f Avg:%5.1f Min:%5.1f Max:%5.1f %n", reduce, mon.getLabel(), mon.getLastValue(), mon.getAvg(), | |
mon.getMin(), mon.getMax()); | |
} | |
private static void useReduceAndAccumulator(final List<Test> list) { | |
final Monitor mon = MonitorFactory.start("Total+reduce2"); | |
final BigDecimal reduce = list.stream().map(Test::getValue).reduce(BigDecimal.ZERO, BigDecimalAccumulator.INSTANCE); | |
mon.stop(); | |
System.out.printf("%10.0f in %13s %5.1f Avg:%5.1f Min:%5.1f Max:%5.1f count %n", reduce, mon.getLabel(), mon.getLastValue(), mon.getAvg(), | |
mon.getMin(), mon.getMax()); | |
} | |
private static void useTotalAndForEach(final List<Test> list) { | |
final Monitor mon = MonitorFactory.start("Total+forEach"); | |
final Total total = new Total(); | |
list.stream().forEach(t -> total.add(t.getValue())); | |
mon.stop(); | |
System.out.printf("%10.0f in %13s %5.1f Avg:%5.1f Min:%5.1f Max:%5.1f %n", total.getTotal(), mon.getLabel(), mon.getLastValue(), | |
mon.getAvg(), mon.getMin(), mon.getMax()); | |
} | |
private static void useTotalAndMap(final List<Test> list) { | |
final Monitor mon = MonitorFactory.start("Total+Map"); | |
final Total total = new Total(); | |
final Iterator<BigDecimal> it = list.stream().map(Test::getValue).iterator(); | |
while (it.hasNext()) { | |
total.add(it.next()); | |
} | |
mon.stop(); | |
System.out.printf("%10.0f in %13s %5.1f Avg:%5.1f Min:%5.1f Max:%5.1f %n", total.getTotal(), mon.getLabel(), mon.getLastValue(), | |
mon.getAvg(), mon.getMin(), mon.getMax()); | |
} | |
private static void useTotal(final List<Test> list) { | |
final Monitor mon = MonitorFactory.start("Total+Loop"); | |
final Total total = new Total(); | |
for (final Test t : list) { | |
total.add(t.getValue()); | |
} | |
mon.stop(); | |
System.out.printf("%10.0f in %13s %5.1f Avg:%5.1f Min:%5.1f Max:%5.1f %n", total.getTotal(), mon.getLabel(), mon.getLastValue(), | |
mon.getAvg(), mon.getMin(), mon.getMax()); | |
} | |
public static class ToTotalCollector implements Collector<BigDecimal, Total, Total> { | |
@Override | |
public Supplier<Total> supplier() { | |
return Total::new; | |
} | |
@Override | |
public BiConsumer<Total, BigDecimal> accumulator() { | |
return Total::add; | |
} | |
@Override | |
public Function<Total, Total> finisher() { | |
return Function.identity(); | |
} | |
@Override | |
public BinaryOperator<Total> combiner() { | |
return (t1, t2) -> { | |
t1.add(t2); | |
return t1; | |
}; | |
} | |
@Override | |
public Set<Characteristics> characteristics() { | |
return Collections.unmodifiableSet(EnumSet.of(IDENTITY_FINISH, CONCURRENT)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment