Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Monday, 26 May 2014

How to efficiently add BigDecimals

Anyone who deals with monetary values knows that double/float won't cut the mustard and if you deal with prices and FX rates, then BigDecimal is the only real option.

This comes with a lot of potential issues, BigDecimal methods do not handle null very well (i.e. not at all) and sometimes a bug crops up because BigDecimal returns new instances.


So the ObjectLabKit Util package will help, but here is a question for you... what is an efficient way to sum a list of BigDecimal coming from a Class.

Assume that we have a list of 500 Test instances and that we need to sum the Test.value and that value could be null.

We shall run the test 1,000 times.


Option 1: Use Total in a for loop

Option 2: Use Total with java8 forEach

Option 3: Use Total and java8 map()

Option 4: Use Java8 map and reduce

Option 5: Use Java8 map, reduce and accumulator

Option 6: Use Java8 and home-made Collector

Option 7: Use Java8 and ObjectLabKit Calculator

Option 8: Use Java8 and Parallel Stream



So what are the results?

On my 2012 MacBook Pro for a list of 500 Test instances.
AlgoAverage (ms)Min (ms)Max (ms)
Use Total in a for loop0.104
Use Total with java8 forEach0.1040
Use Total and java8 map()0.106
Use Java8 map and reduce002
Use Java8 map, reduce and accumulator002
Use Java8 and home-made Collector0.106
Use Java8 and ObjectLabKit Calculator002
Use Java8 and Parallel Stream0.1010


First of all, the value generated is the same for every algo, so no bug there it seems.

The results are quite similar except for the Max value, implying a greater deviation in the results. I've used JAmon for measuring min/max and average time.

Surprisingly, it seems that forEach has at least 1 execution at 40ms, which is way above the rest. Otherwise using the ObjectLabKit Calculator seems a good compromise between having to write the reduce correctly (! watch out if the BigDecimal on the right is null!) and using the raw map/reduce. 

The Parallel Stream is not as efficient, as it takes some time to coordinate the tasks and split the list. let's see if it gets any different with more data. 

On my 2012 MacBook Pro (QuadCore) for a list of 50,000 Test instances and the parallelStream is then becoming the most efficient.

AlgoAverage (ms)Min (ms)Max (ms)
Use Total in a for loop1020
Use Total with java8 forEach1.1048
Use Total and java8 map()2.1140
Use Java8 map and reduce1.219
Use Java8 map, reduce and accumulator1.2110
Use Java8 and home-made Collector1.4112
Use Java8 and ObjectLabKit Calculator1.2111
Use Java8 and Parallel Stream0.6017


So it looks like, when using single thread, that the RAW use of stream.map and reduce is the most efficient but one has to remember how to write it:

  final BigDecimal reduce = list.stream()
        .map(Test::getValue)
        .reduce(BigDecimal.ZERO
                (a, b) -> b != null ? a.add(b) : a);

Using the parallelStream (when suitable) reduces the average to 0.5ms but the max is 18ms
  final BigDecimal reduce = list.parallelStream()
        .map(Test::getValue)
        .reduce(BigDecimal.ZERO
                (ab) -> b != null ? a.add(b) : a);


Full code available here at GitHub Gist

Sunday, 29 April 2007

JTreeMap 1.1.0 released!


Hi *,

ObjectLab is very pleased to announce the immediate release of JTreeMap 1.1.0, a heatmap/treemap visual library for JDK 5.0. We believe this is the only open source library of this kind under a business friendly license.

Towards the end of 2006, ObjectLab got involved with JTreeMap as part of a financial application. Laurent Dutheil had been developing it but found it more time consuming that anticipated. It is where ObjectLab offered some expertise and the result is the new release 1.1.0. We acknowledge and thank Laurent for his great contribution.

Home page: http://jtreemap.sourceforge.net

The release is available on SF and on 2 Maven repositories (whilst we’re going through the process of adding it in the official repository). The ObjectLab Open Source repositories are:

http://objectlabkit.sourceforge.net/m2-repo

and

http://objectlabkit.sourceforge.net/m1-repo

The question of how to represent and visualize a lot of information at a glance is a hot topic in IT. A Treemap, also known as Heatmap, is an important tool for this. A TreeMap graphically represents a hierarchical structure.

Typically, the hierarchy will involve a tree of nodes of different sizes and different colours. The size and colours are determined by parameters such as the relative importance of a node in comparison to the full size. A well known examples is the Map of the Market on www.smartmoney.com but their library is not open source (and very pricey!).

JTreeMap comes in 2 flavours: JTreeMap for Swing and KTreeMap for SWT.

Enjoy!

Benoit & the rest of the ObjectLab Team.

Thursday, 19 April 2007

Coming QALab: Emma, XML FindBugs, new XML, new Chart & more!

I thought that it may be interesting to do something that Microsoft has been doing for years, like announcing a coming release of something... The difference is that, this is not vapo(u)rware, it will turn up!

http://qalab.sourceforge.net.

So, what are the items we're working on?

  • Emma support is coming, thanks to the contribution of Robert Crawford.

  • Support for the XML output from Findbugs (rather than the xdoc one), some people had to run findbugs twice due to this limitation.

  • A new Spider Chart will show 3 snapshots of multi-dimensional data at 0, 30 and 90 days. You'll be able to see what is happening at a glance!

  • Automatic migration of the QALab XML to a new format that will also add the notion of "project" and "module" making it more hierarchical... one step closer to aggregation!

  • a bit of code re-org to keep the house tidy!

  • Some experimental work with storing QALab stats in a DB


So... when will it be available? Like cooking, it will be ready when it's ready... but for the impatient type, all these features are in Subversion!

Finally, we're willing to add more tools and are welcoming contributions, especially for Clover! Should not be difficult... Our main problem is that there are only 24 hours in a day!

Until next time,

Benoit

Monday, 9 April 2007

StatSVN Demos are updated

StatSVN is pleased to announce an update of the demo site.

http://www.statsvn.org/demo

StatSVN demo is designed to show StatSVN capabilities for some well-known open source projects. Some of those are rather large and required some serious initial computing power (first run is rather big if the project has a lot of history).

At the moment, we have selected:

  1. Subversion itself!

  2. Spring Rich Client (java rich client library from Spring)

  3. Hibernate 3 (java persistence)

  4. Joda Time (cool java time library)

  5. Log4J (Apache logging library for Java)

  6. Ruby (yes, the language, in C!)

  7. Groovy

  8. Grails

  9. Felix (Apache OSGi implementation)

  10. Ant (Apache Ant, had more than 500,000 revisions)

  11. Mule (The World's most widely used open source ESB)

  12. Synapse (Apache Synapse, Synapse is a mediation framework for Web Services. Synapse allows messages flowing through, into, or out of an organization to be mediated.)

  13. QPid (Apache QPid, implementation of AMQP, Advanced Messaged Queuing Protocol)

  14. QALab A great tool for keeping track of QA statistics over time.

  15. PZFileReader Parsing of delimited strings or fixed-length strings.

  16. ObjectLab Kit Date Calculators for Business and Finance.

  17. JTreeMap Heat Map library.

  18. AntLibs DotNet.

  19. AntLibs HTTP.

  20. AntLibs Manual for Eclipse.

  21. AntLibs Subversion.

  22. AntLibs Ant Unit.

  23. XStream XStream is a simple library to serialize objects to XML and back again.

  24. JBoss AS No less! Do not try this at home, the first batch of statistics required 102,000 calls to the server... now it is fast!

  25. JRuby JRuby is an 100% pure-Java implementation of the Ruby programming language!

  26. ServiceMix The Apache ServiceMix project is an ESB and component suite based on the Java Business Interface (JBI) standard - JSR 208

  27. Commons-Lang The Apache Commons Lang.

  28. JDK7 The java JDK version 7! Checkout the very latest changes!

  29. TestNG TestNG, the new testing framework!

  30. Maven 1 The Apache Maven 1!

  31. Maven 2 The Apache Maven 2!

  32. Maven 2 Plugins The Apache Maven 2 Plugins!

  33. Continuum The Apache Continuum project!

  34. Pebble a Blog software!



If you would like us to add an open source project, please raise an issue on SF and provide us with the following information:


  1. Project Name

  2. Project Home page

  3. A one paragraph of html describing the project

  4. URL to anonymous Subversion server for the trunk

  5. List of tags, versions that should be identified.

  6. A list of developers, loginId-RealName, URL to homepage, URL to photo and email (all optional)

We cannot guarantee inclusion but you never know...

StatSVN Team.

PS: Support us by checking the Ads on the demo site. Thanks.