Scala vs. JavaScript Performance for BBP

Basically I have an implementation of the BBP algorithm to calculate arbitrary hex digits of Pi. Thanks to this blog post I was able to write a version in Scala, and then another version in JavaScript.

I modified the algorithm to split the calculation into equal sizes (2,500,000 loop iterations). You can look at both versions here.

I was quite surprised when I did a first test run on node.js (v0.10.7). The JS version was just a few seconds behind the Scala version per task. What I expected was an order of magnitude, not just a bit.

The tests look like this and are generated by the Scala version (with the Scala time filled in).

var start = Date.now(); console.log(arrays_equal(task(1, 10000000, 1, 0.0), [2500001, 0.9343597446633509, false]), 'Scala: 9907 vs JavaScript: ' + (Date.now() - start));

The parameters are actual ones that arise when calculating the billionth digit.

Here are the node results:

true 'Scala: 9671 vs JavaScript: 11472'
true 'Scala: 9907 vs JavaScript: 11090'
true 'Scala: 9938 vs JavaScript: 11110'
true 'Scala: 9975 vs JavaScript: 10970'
true 'Scala: 9704 vs JavaScript: 10744'
true 'Scala: 9663 vs JavaScript: 10822'
true 'Scala: 9626 vs JavaScript: 10787'
true 'Scala: 9701 vs JavaScript: 10785'
true 'Scala: 9358 vs JavaScript: 10405'
true 'Scala: 9289 vs JavaScript: 10417'
true 'Scala: 9329 vs JavaScript: 10507'
true 'Scala: 9407 vs JavaScript: 10404'
true 'Scala: 8536 vs JavaScript: 9472'

I was even more surprised when I let the code run in the latest version of Chrome (30.0.1599.69). Here the JS version was faster than the Scala version!

true "Scala: 9671 vs JavaScript: 9435"
true "Scala: 9907 vs JavaScript: 9124"
true "Scala: 9938 vs JavaScript: 9096"
true "Scala: 9975 vs JavaScript: 9094"
true "Scala: 9704 vs JavaScript: 8905"
true "Scala: 9663 vs JavaScript: 8906"
true "Scala: 9626 vs JavaScript: 8917"
true "Scala: 9701 vs JavaScript: 8907"
true "Scala: 9358 vs JavaScript: 8687"
true "Scala: 9289 vs JavaScript: 8647"
true "Scala: 9329 vs JavaScript: 8656"
true "Scala: 9407 vs JavaScript: 8671"
true "Scala: 8536 vs JavaScript: 7908"

I wonder if there are any obvious bottle necks in the Scala version.


© 2022 Florian Klampfer

Powered by Hydejack v9.1.6