0

我想知道(大概)读取存储在 Android SD 卡上的大文件(50MB 到 100MB)需要多少时间。我在 Google Nexus One 上的 Android 2.3.3 上使用以下代码。这会给我一个合理的估计吗?有没有我应该使用的标准方法?此外,使用本机代码会提高我的文件 I/O 性能吗?

谢谢。

public void readFileFromSDCard() throws IOException
{
    long start = System.currentTimeMillis();
    long size = 0;
    File file = new File(OVERLAY_PATH_BASE + "base-ubuntu.vdi");
    try {

        InputStream in = new FileInputStream(file);
        try {
            byte[] tmp = new byte[4096];
            int l;
            while ((l = in.read(tmp)) != -1) {
                size = size + 4096;
            }
        } finally {
            in.close();
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } 
    long end = System.currentTimeMillis();
    Log.e(LOG_TAG,
            "Reading file " + file.getAbsolutePath() + " with " + size  + "bytes took " + (end-start) + " ms.");

}
4

1 回答 1

1

对这段代码最简单的做法是计算每次 while 循环迭代需要多长时间,并使用它来估计还剩多长时间。例如,如果您在一秒钟内一次迭代中处理 0.1Mb 并且总共有 1Mb 要做,那么您可以猜测您还有大约 9 秒的时间。

SD 卡性能在不同手机之间差异很大,即使在同一部手机上速度也可能无法预测(例如,我的手机上的小文件随机延迟很大)。我认为不值得我自己做任何比上述更复杂的事情,例如在处理文件之前对手机进行基准测试。对于大多数用途来说,一个百分比条可能就足够了。

于 2011-04-06T11:29:04.863 回答