估算 Android 上从 SD 卡读取大文件的时间

发布于 2024-10-30 19:22:34 字数 916 浏览 1 评论 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.");

}

I want to get an idea (ballpark) of how much time it takes to read a large file (50MB to 100MB) stored on Android's SD card. I'm using the following code on Android 2.3.3 on a Google Nexus One. Will this give me a reasonable estimate ? Is there a standard method that I should be using ? Also, will using native code improve my file I/O performance ?

Thanks.

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.");

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怎会甘心 2024-11-06 19:22:34

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

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

The easiest thing to do for this code would be to time how long each while loop iteration takes and use that to estimate how long is left. For example, if you process 0.1Mb in one iteration in one second and you have 1Mb in total to do, you can guess that you have about 9 seconds to go.

SD card performance varies a lot between different phones and the speed can be unpredictable even on the same phone (e.g. I get large random delays on my phone for small files). I don't think it's worth doing anything more sophisticated than the above myself, such as benchmarking the phone before processing the file. A percent bar is probably enough for most uses.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文