SpeedUp from Charts with Streams

Today we I am writing about the possibillity to speedup the JavaFX GUI elements with the new streams-API from JDK8.
Let´s play with the original example from Oracle. The LineChart demo will be used as base for the following. We are collecting/generating values for a line of points for every hundreds step. Al steps between are interpolated with splines. For calculating the splines I am using the jakarta-commons math3 lib. [1]
Normaly the following steps are needed to generate a LineChart. - get the base values to show
- calculate the interpolated values
- create the line-elements
- fill the Line-Chart and show it

Get the base values to show
For this example we are generating the values we are needing. To hold the values for a List of curves I am using a Without streams you could do it like this:
But with streams I could write this in a more compact form. The nice here is the possibility to concat the single steps. But this is only to show the new streams api. There is no parallel part. Calculate the interpolated values Now I will start to caclculate the interpolated values. For this I am using the commons-math3 lib from Apache. The way to do this is short and straid forward. - Get the base values from the curve you want to calculate on
- Choose the algorithm you want to use
- Fill the base values as init into the function
Now we are able to caculate the interpolated values. This is something we can do in parallel for every curve. This means that we can start with the speed up. The importand parts are the ".parallelStream()" to start a Thread for eveery elements of the List and during the "map(v-> ... )" phase the calculation is done. Every task is put to the Root - Fork-And-Join Pool. Create the line-elements The grafical elements for the Line-Chart are created in the same way. In parallel for every curve. Fill the Line-Chart and show it Now the last step is the filling of the Line-Chart itself. This is done in a seriel way. No parralel way is possible in the moment. But this is not the part that could bring a speed-up. Lesson Learned What we can see is the possibillity to speed up the GUI part in several ways. Not only the generating of the values itself. The creation of the grafical elements could be done in parralel too. The seriell Version LineChartSerialDemo, needed for the 11´th round 2.799.209.417ns and the parralel version LineChartDemo was done in 261.545.220ns. This is a speed up of 10 at my MacBookPro. Happy coding..

Kommentare

Beliebte Posts