Scala operations with arrays performance (scalacl plugin)
Asked Answered
P

1

7

Are there any cons of using scalacl plugin?

I am planning to use scala in my project. I have written a little bit of code in scala to see its time of execution.

(1 to 1000000).map(1 + _).sum

1. Without plugin

is compiled to something like this:

BoxesRunTime.unboxToInt(((TraversableOnce)Predef..MODULE$.intWrapper(1).to(1000000).map(new MyScala..anonfun.1(), IndexedSeq..MODULE$.canBuildFrom())).sum(Numeric.IntIsIntegral..MODULE$));

and run in about 375 ms

2. With scalacl plugin

 int i = 1;
 int j = 1000000;
 int k = j;
 int m = i;
 for (VectorBuilder localVectorBuilder = new VectorBuilder(); m <= k;) {
     int n = m;
     localVectorBuilder.$plus$eq(BoxesRunTime.boxToInteger(1 + n));
     m += 1;
 }
 int a =  BoxesRunTime.unboxToInt(localVectorBuilder.result().sum(Numeric.IntIsIntegral..MODULE$));

259 ms

Parliamentarian answered 13/12, 2011 at 22:44 Comment(2)
Improvement of 30% is not much. I optimized one part of my code, which is now using arrays and while loops, for a 100x speed-up. Idiomatic Scala can be really slow. For example if you get rid of the boxing, then you will get something more impressive.Barahona
Btw, Range#sum is now optimized in trunk and runs in constant time O(n) instead of linear O(n). Algorithmic improvements are most of the time preferable.Interurban
C
10

Possible cons I can think of:

1) The loop optimization appears to work and the developer seems very competent, but it says in bold letters in the "About ScalaCL" screen "ScalaCL is not production-ready !". In other words, there's a small chance you might introduce bugs and instability

2) You need to remember to compile with the plugin every time, else you may suddenly find you have performance issues. You can't be sure that the plugin will be maintained / compatible in the medium or long-term

3) You might become reliant on its optimisations, leading you to write inefficient code, whereas identifying and hand-optimizing the bottlenecks might lead to faster code overall. In other words, it can in effect "paper over the cracks"

4) It's an extra library dependency and adds complexity to your build files

You asked for cons, but these are pretty minor compared to its pros. Personally I'd have no hesitation about using the loop-optimisations for personal projects; not so sure about the cl-collections just yet (I tried them and found my GPU a bit slower than my CPU - obv it's dependent on available hardware), but I think the project has a great future, whether on its own or incorporated into the standard compiler and libraries. I've seen some very dramatic speedups (up to like 20x faster) for some code.

Cauline answered 13/12, 2011 at 23:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.