What is the fastest way to process large amounts of data in Android?
Can you suggest what is the most efficient way for me (Android/JAVA newbie) to deal with relatively large amounts of data?
I need to compute something for each of the 1000…5000 elements, say a large data type (x1, y1, z1 – double, flag1…). flagn – boolean value, desc1… descn – string) often (once per second), which is why I want to do it as fast as possible.
Which way is best? Want to declare a multidimensional array, or generate an array for each element (x1[i], y1[i]…), a special class, some kind of JavaBean? Which one is the most effective in terms of speed, etc.? What is the most common way to handle this kind of thing in Java?
Thank you very much!
Solution
Nick, you asked a very general question. I’ll try to answer it, but note that if you want something more specific, you’ll need to dig deeper into your question.
Some reverse envelope calculations indicate that for an array containing 5000 doubles, you would use 8 bytes * 5000 = 40,000 bytes or approximately 40 kB of memory. That’s not too bad, as most Android devices have memory on the order of megabytes or even gigabytes. A good ‘ol ArrayList should store this data well. You can speed things up by specifying the length of the ArrayLists at constructor time. This way, the Arraylist doesn’t have to scale dynamically each time more data is added to it.
But be careful. Since we are on memory-constrained devices, what can happen is that if you generate a large number of ArrayLists in quick succession, you may start triggering the garbage collector frequently. This can cause your app to slow down (and actually the entire device). If you really want to generate a lot of data, don’t store it in memory. Store it on disk so that you have enough space and don’t trigger the garbage collector all the time.