Android Performance: Java vs Xamarin vs Xamarin.Forms Part 2
Here’s the second part of the post from the last week. Just to remind you, I was comparing performance of Java, Xamarin and Xamarin.Forms. I was focusing on loading times and how good UI and animations are working. Today I will show you results from working with files, sqlite and conversions.
Test 1: Saving Big Files
Here I was saving 10 MB files 10 times (each as a new file) in a loop. Each test here was done twice, because it was often the case that for Xamarin second try was much faster (and then consecutive tries were more or less the same).
Development Platform | Time [Seconds] (First try) | Time [Seconds] (Second try) |
---|---|---|
Java | 47.959 | 45.520 |
Xamarin | 4.666 | 3.019 |
Xamarin.Forms | 2.994 | 2.764 |
Yes, that is right, Xamarin.Forms was the fastest here! Although Xamarin was not much behind, especially on the second try. Java time is very surprising. As I said in the previous post, I’m not a Java expert and I simply took popular answer from StackOverflow, so I think it is standard way of doing this.
Test 2: Saving Small Files
Now we are saving 1000 10 KB files, similarly as in the previous test.
Development Platform | Time [Seconds] (First try) | Time [Seconds] (Second try) |
---|---|---|
Java | 1.104 | 0.676 |
Xamarin | 0.692 | 0.501 |
Xamarin.Forms | 0.694 | 0.584 |
Java is still the slowest, but the difference is not that big this time. On the second try we can say that results are almost equal. Xamarin and Xamarin.Forms are very similar on both runs with small advantage of pure Xamarin.
Test 3: Loading Big Files
We take the 10 MB files that we saved before, load them and save in a list. The exception is Java where I was getting OOM.
Development Platform | Time [Seconds] (First try) | Time [Seconds] (Second try) |
---|---|---|
Java | 4.143 | 4.138 |
Xamarin | 1.949 | 1.839 |
Xamarin.Forms | 1.876 | 1.761 |
Looks like Java is not that good with files. Xamarin is more than 2 times faster. And again, Xamarin.Forms is slightly better.
Test 4: Loading Samll Files
We load 1000 10 KB files that we saved before and put them on a list.
Development Platform | Time [Seconds] (First try) | Time [Seconds] (Second try) |
---|---|---|
Java | 2.054 | 2.048 |
Xamarin | 0.950 | 0.478 |
Xamarin.Forms | 0.961 | 0.568 |
Results are pretty much the same as with loading big files. Java is again 2 times slower than competitors and on the second try even 4 times slower. This time only, Xamarin is a bit faster than Forms.
Test 5: SQLite Insert
We are inserting here 1000 items to the database. With Java we use SQLiteOpenHelper. With Xamarin we used SQLite-Net package, which is probably the most popular way to do this with Xamarin. However I also tried SQLiteOpenHelper with Xamarin to see how it compares to Java.
Development Platform | Time [Seconds] (First try) | Time [Seconds] (Second try) |
---|---|---|
Java (SQLiteOpenHelper) | 1.888 | 1.934 |
Xamarin (SQLite-Net) | 12.249 | 12.250 |
Xamarin.Forms (SQLite-Net) | 12.300 | 11.757 |
Xamarin (SQLiteOpenHelper) | 2.729 | 2.751 |
Ok, so Java is finally faster! And it is much faster than Xamarin, even when we use SQLiteOpenHelper. Why is that? Well, I was inserting here items in a loop, without transaction. Let’s see how it looks when we use a transaction.
Development Platform | Time [Seconds] (First try) | Time [Seconds] (Second try) |
---|---|---|
Java (SQLiteOpenHelper) | 0.127 | 0.142 |
Xamarin (SQLite-Net) | 0.156 | 0.106 |
Xamarin.Forms (SQLite-Net) | 0.125 | 0.107 |
It is very different now. Java and Xamarin.Forms are equal on the first try, Xamarin is a bit slower. On the second try both Xamarin and Forms are faster, but overall I would say they are all very similar.
There’s also InsertAll method in SQLite-Net, so I wanted to try it too. Results are pretty much the same as with transaction.
Development Platform | Time [Seconds] (First try) | Time [Seconds] (Second try) |
---|---|---|
Xamarin (SQLite-Net) | 0.135 | 0.100 |
Xamarin.Forms (SQLite-Net) | 0.098 | 0.097 |
Test 6: Read from SQLite
We are reading 1000 records that we have put before. In Java we use 2 methods: query and rawQuery (in sql) from SQLiteOpenHelper. In Xamarin and Forms we use linq and query in sql. For Xamarin I also checked query and rawQuery from SQLiteOpenHelper.
Development Platform | Time [Seconds] (First try) | Time [Seconds] (Second try) |
---|---|---|
Java (query) | 0.041 | 0.038 |
Xamarin (linq) | 0.176 | 0.060 |
Xamarin.Forms (linq) | 0.156 | 0.056 |
Java (rawQuery) | 0.035 | 0.032 |
Xamarin (sql query) | 0.083 | 0.062 |
Xamarin.Forms (sql query) | 0.068 | 0.072 |
Xamarin (SQLiteOpenHelper – query) | 0.483 | 0.355 |
Xamarin (SQLiteOpenHelper – rawQuery) | 0.384 | 0.419 |
First 3 rows are probably the most popular ways of querying database and we can see that linq in Xamarin is much slower on the first try than query in Java. Even second try, when linq is much faster, is still worse.
Second method from Java, rawQuery, is a little bit faster than query, but there’s no huge difference. Sql query in Xamarin is faster than linq only on the first try, later they are pretty much the same.
Surprisingly, query and rawQuery in Xamarin is much worse than in Java and much worse than linq. It appears that SQLite-Net package and linq are the best tools for working with SQLite in Xamarin.
Test 7: Enum to string conversion
Now we are switching to conversions and we are starting with enum to string. We do it 1 mln times in a loop.
Development Platform | Time [Seconds] (First try) | Time [Seconds] (Second try) |
---|---|---|
Java | 0.006 | 0.015 |
Xamarin | 3.089 | 2.668 |
Xamarin.Forms | 2.706 | 2.744 |
Numbers say it all, Java is amazingly fast here, Xamarin and Forms are similar.
Test 8: String to enum conversion
Here we parse 1 mln strings to enums.
Development Platform | Time [Seconds] (First try) | Time [Seconds] (Second try) |
---|---|---|
Java | 0.340 | 0.371 |
Xamarin | 2.881 | 2.633 |
Xamarin.Forms | 2.881 | 2.801 |
Java is still a very clear winner.
Test 9: String to int conversion
We parse 1 mln strings to ints.
Development Platform | Time [Seconds] (First try) | Time [Seconds] (Second try) |
---|---|---|
Java | 0.272 | 0.267 |
Xamarin | 0.523 | 0.553 |
Xamarin.Forms | 0.547 | 0.494 |
Xamarin is much closer to Java this time, but it is still about 2 times slower.
Summary
These are all test that I have prepared. Xamarin is doing really good job with working with files. It is also good with inserting to database and a bit worse than Java in reading records. It is however significantly worse in conversions. Good thing is that we don’t usually do them in such huge amounts, so it is probably a minor problem.
Looking at the above results and the results from the previous part I think that Xamarin is good choice for developers. Performance is fine most of the time and we have all nice features of C#, which is huge advantage (at least for people who already know C#). Forms can still lack in performance as for loading times, so it’s not perfect for all use cases. If we make something simple or one code for UI for all platforms is more important than great performance than Forms will be good choice too.