Scala on Android - Performance

View: New views
3 Messages — Rating Filter:   Alert me  

Scala on Android - Performance

by akshaydashrath :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

Thank you,

Regards,

Akshay


Re: [scala] Scala on Android - Performance

by Emil H :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I have very little knowledge on this, but just a thought. Have you tried to run a similar test of the code using the regular JRE and not the Dalvik engine? If the same performance difference is present when using the JRE, then maybe it has something to do with your code. 

I noticed that you in the binarySearch method do some matching, but ignore the result and then have a guards that chooses which execution path to take. This seems strange to my eyes, why not use plain if statements? Not sure if there would be any performance impact, but to me it would look nicer  :)

 - Emil H

On Sun, Nov 1, 2009 at 5:52 PM, akshaydashrath <akshaydashrath@...> 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

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.



Re: [scala] Scala on Android - Performance

by David Hall-17 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sun, Nov 1, 2009 at 9:52 AM, akshaydashrath <akshaydashrath@...> 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.
>
>