Know more and connect with me on [Linkedin Profile].

Tuesday, February 05, 2019

Android Performance Story

I am developing a personal finance application and it uses SQLite to store all of its data. I had slowness in the application where I calculate balance for all accounts.

The first thing that came to my mind is that the DB access is slow and I have to research if I should add indexes to the tables. Before doing that, I gave Android Studio Profiler a try.

What was a surprise to me is finding the slowness is not related to database access but to date conversion function. See:

See this code snippet,

Class DateUtils {

const val YYYY_MM_DD_HH_MM_SS_S = "yyyy-MM-dd HH:mm:ss"
private val formatter = SimpleDateFormat(YYYY_MM_DD_HH_MM_SS_S, Locale("en"))

fun toDate(dateString: String) : Date  = formatter.parse(dateString)


Although the formatter object is created once and reused by the toDate() function, the toDate() is extremely slow. Although it is being called for each fetched row from SQLite, I did not expect it to be the performance bottleneck. I always assume DB access is the slowest part of any application which, in my little story, is wrong. 

So I learned, do not assume, get data and then decide.

No comments: