Android Performance: Java vs Xamarin vs Xamarin.Forms Part 2

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 PlatformTime [Seconds] (First try)Time [Seconds] (Second try)
Java47.95945.520
Xamarin4.6663.019
Xamarin.Forms2.9942.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 PlatformTime [Seconds] (First try)Time [Seconds] (Second try)
Java1.1040.676
Xamarin0.6920.501
Xamarin.Forms0.6940.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 PlatformTime [Seconds] (First try)Time [Seconds] (Second try)
Java4.1434.138
Xamarin1.9491.839
Xamarin.Forms1.8761.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 PlatformTime [Seconds] (First try)Time [Seconds] (Second try)
Java2.0542.048
Xamarin0.9500.478
Xamarin.Forms0.9610.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 PlatformTime [Seconds] (First try)Time [Seconds] (Second try)
Java (SQLiteOpenHelper)1.8881.934
Xamarin (SQLite-Net)12.24912.250
Xamarin.Forms (SQLite-Net)12.30011.757
Xamarin (SQLiteOpenHelper)2.7292.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 PlatformTime [Seconds] (First try)Time [Seconds] (Second try)
Java (SQLiteOpenHelper)0.1270.142
Xamarin (SQLite-Net)0.1560.106
Xamarin.Forms (SQLite-Net)0.1250.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 PlatformTime [Seconds] (First try)Time [Seconds] (Second try)
Xamarin (SQLite-Net)0.1350.100
Xamarin.Forms (SQLite-Net)0.0980.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 PlatformTime [Seconds] (First try)Time [Seconds] (Second try)
Java (query)0.0410.038
Xamarin (linq)0.1760.060
Xamarin.Forms (linq)0.1560.056
Java (rawQuery)0.0350.032
Xamarin (sql query)0.0830.062
Xamarin.Forms (sql query)0.0680.072
Xamarin (SQLiteOpenHelper – query)0.4830.355
Xamarin (SQLiteOpenHelper – rawQuery)0.3840.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 PlatformTime [Seconds] (First try)Time [Seconds] (Second try)
Java0.0060.015
Xamarin3.0892.668
Xamarin.Forms2.7062.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 PlatformTime [Seconds] (First try)Time [Seconds] (Second try)
Java0.3400.371
Xamarin2.8812.633
Xamarin.Forms2.8812.801

 

Java is still a very clear winner.

 

Test 9: String to int conversion

We parse 1 mln strings to ints.

 

Development PlatformTime [Seconds] (First try)Time [Seconds] (Second try)
Java0.2720.267
Xamarin0.5230.553
Xamarin.Forms0.5470.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.