Android 最佳缓冲区大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10143731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:39:34  来源:igfitidea点击:

Android Optimal Buffer Size

androidbuffer

提问by L7ColWinters

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?

在 java 中有一个 32 Kb 的最佳缓冲区大小,它完全基于所使用的 CPU 架构。在 Android 手机上,Dalvik VM 是否动态地知道 CPU 的正确缓存,以获得独立于许多不同手机的最大缓冲区大小?如果是这样,我将如何在运行时弄清楚?

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?

假设我想通过使缓冲区最大和最快来优化录音活动。我知道你可以获得最小的尺寸,但最佳尺寸呢?

回答by Joon Hong

Maybe it depends on what device you have or mind.

也许这取决于您拥有或介意的设备。

However, experimentally, 8K < buffer size < 32Kdoes 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

但是,在实验上,8K < 缓冲区大小 < 32K效果很好,并且在 8K 下有显着的性能改进。有趣的是,一些缓冲区大于 64K 的数据表现出比缓冲区低于 64K 的数据更差的性能

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

(我已经在多个 android 设备上进行了测试,并尝试读取具有各种缓冲区大小的 20MB 二进制文件。)

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

这是 exp 结果,如果您想以漂亮的形式转换数据,最好将它们粘贴到电子表格中。标头表示缓冲区大小,单位为毫秒

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

图: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;


enter image description here

在此处输入图片说明