L7ColWinters L7ColWinters - 12 days ago 7
Android Question

Android Optimal Buffer Size

In java there is an optimal buffer size of 32 Kb which is based solely on the cpu architecture being used. On Android phones does the Dalvik VM dynamically know the proper cache of the cpu to get the largest buffer size independent of the many different phones out there? If so how would I figure that out at runtime?

Say I want to optimize a audio recording activity by making the buffer the largest it can be and also the fastest. I know you can get the minimal size for it but what about the optimal size?

Answer

Maybe it depends on what device you have or mind.

However, experimentally, 8K < buffer size < 32K does work well and there are significant performance improvements under 8K. Somewhat interesting is that some data with buffer > 64K showed poorer performance than data with under 64K buffer

(I've tested on several android devices and tried to read 20MB binary file with various buffer size.)

Here's exp result and you'd better to paste them to spreadsheet if you wanna convert data in pretty form. header means buffer size and units are millisecond

graph: http://fb.com/photo.php?fbid=468345876512381

                   128     256     512     1K      2K      4K      8K      16K     32K     64K     128K    256K    512K    1M      2M      4M      8M      16M
    Galaxy S       4047    3060    269     155     100     65      64      52      51      45      47      50      49      43      44      46      45      58
    Optimus LTE    1178    617     322     172     101     65      47      42      41      35      36      39      44      61      56      51      72      60
    HTC EVO        3971    1884    941     480     251     141     95      69      56      50      48      55      50      49      48      48      48      47         
    Galaxy S2      750     383     210     123     74      50      41      37      35      34      34      37      39      44      46      44      45      44
    Galaxy Nexus   2272    1216    659     341     187     108     70      52      41      38      38      45      44      54      56      66      68      58
    Galaxy Note    1549    799     404     220     127     75      58      54      52      56      52      45      44      62      43      39      44      46

InputStream in = openFileInput(FILE_NAME);
startTime = System.currentTimeMillis();

while (in.read(buffer) > 0) {
    readCount++;
}

elapsedTime = System.currentTimeMillis() - startTime;