Re: Scala on Android - Performance
David Hall <dlwh <at> cs.berkeley.edu>
2009-11-02 00:14:54 GMT
On Sun, Nov 1, 2009 at 9:52 AM, akshaydashrath <akshaydashrath <at> gmail.com> wrote:
>
> Hey all,
>
> I'm a complete newbie to programming on the Scala platform, however I've
> been working on testing the performance of Scala on the Android platform. I
> have started a project on github which can be found at
> http://github.com/akshaydashrath/Scala-Performance-Testing-Android,
> basically what I'm testing is the performance of a quick sort as well as a
> binary search on a large number of elements present in a list first with
> Scala and then with Java. I expected results to favour Scala but I'm getting
> worse off performance with Scala then with Java, some of the initial results
> are given below. Since I have little knowledge with the way scala code is
> compiled into byte code I thought maybe someone could explain this for me,
> Is the compilation of code designed to make best use of VM features such as
> JIT and adaptive compilation, this would explain the lack of performance on
> the Dalvik VM as it has none such optimisations. Any help would be most
> appreciated as the results of this study will be presented at Droidcon
> London and any help will be duly noted and credit provided in the
> presentation.
>
> Results:
>
> Scala:
> Sorting = 1219msec
> Searching time = 37msec
> Total instructions executed: 1397082
> Method invocations: 183121
>
> Java:
> Sorting = 343msec
> Searching time = 4msec
> Total instructions executed: 895190
> Method invocations: 75442
>
At least in one place you're comparing data structures and algorithms
but not languages. Your scala implementation of quicksort is the sort
of "canonical" quicksort you see for functional languages, but it uses
Lists and not Arrays, like your Java implementation does. In
particular, your scala partition logic makes three O(n) passes over
each list (because indexing into a list takes time linear in the
index). Your java quicksort, by comparison, is normal imperative
quicksort, which partitions the Array(!) in a single pass.
By way of comparison, the scala library implementation of sort would
copy things to an Array, and then invoke a standard array sort
algorithm, and then copy them back into the expected result type. (And
I'm guessing that if the underlying data structure is already an
Array, Scala is usually smart enough to just sort that array).
So yes, if you write slow code, it will be slow.
-- David
> Thank you,
>
> Regards,
>
> Akshay
>
>
> --
> View this message in context: http://old.nabble.com/Scala-on-Android---Performance-tp26149143p26149143.html
> Sent from the Scala mailing list archive at Nabble.com.
>
>